Skip to content

Instantly share code, notes, and snippets.

@lizettepreiss
Last active October 30, 2018 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lizettepreiss/1c3118a37fd206f2caa29ccbe61e1215 to your computer and use it in GitHub Desktop.
Save lizettepreiss/1c3118a37fd206f2caa29ccbe61e1215 to your computer and use it in GitHub Desktop.
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