Skip to content

Instantly share code, notes, and snippets.

@willwangcc
Created March 5, 2018 23:38
Show Gist options
  • Save willwangcc/91af8fc596e450afd006045671d07676 to your computer and use it in GitHub Desktop.
Save willwangcc/91af8fc596e450afd006045671d07676 to your computer and use it in GitHub Desktop.
used in the article
//Java program to illustrate the
// concept of inheritance
// https://www.geeksforgeeks.org/inheritance-in-java/
// Demo: https://repl.it/@WillWang42/Java-Class-Inheritance-Demo
public class Main
{
// base class
static class ClassA
{
public String name;
public ClassA(String name)
{
this.name = name;
}
// the Bicycle class has three methods
public String print()
{
return(name);
}
}
// derived class
static class ClassB extends ClassA
{
public String surname;
public ClassB(String name, String surname)
{
super(name);
this.surname = surname;
}
@Override
public String print()
{
return (super.print()+
"\n"+ surname);
}
}
public static void main(String args[])
{
ClassA a = new ClassA("calss A");
ClassB b = new ClassB("class B","I'm the child");
System.out.println(a.print()); // class A
System.out.println(b.print()); // class B, "I'm the child"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment