Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Java Inheritance Demo. Which parent's methods in a hierarchy of inheritance get called.
See Child.java, Parent.java, GrandParent.java, GreatGrandParent.java, InheritanceDemo.java
package preiss.inheritancedemo;
public class Child extends Parent{
@Override
public void handle(){
System.out.println("Child.handle");
super.handle();
}
}
package preiss.inheritancedemo;
public abstract class GrandParent extends GreatGrandParent{
@Override
public void doStuff(){
System.out.println("GrandParent.doStuff");
}
}
package preiss.inheritancedemo;
public abstract class GreatGrandParent {
public abstract void doStuff();
public void handle(){
System.out.println("GreatGrandParent.handle");
doStuff();
}
}
package preiss.inheritancedemo;
public class InheritanceDemo {
public static void main(String[] args) {
Child cw = new Child();
cw.handle();
}
}
package preiss.inheritancedemo;
public class Parent extends GrandParent{
@Override
public void doStuff(){
System.out.println("Parent.doStuff");
super.doStuff();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.