Java Inheritance Demo

A simple demonstration of which methods get called at which point when there are many levels of inheritance, some with overridden methods and others without overriding methods.

   Child.java


package preiss.inheritancedemo;

public class Child extends Parent{
	@Override
	public void handle(){
		   System.out.println("Child.handle");
		   super.handle();
	}
}

Child.java hosted on GitHub

   GrandParent.java


package preiss.inheritancedemo;

public abstract class GrandParent extends GreatGrandParent{
	   @Override
	   public void doStuff(){
		   System.out.println("GrandParent.doStuff");
	   }
	      
}

   GreatGrandParent.java


package preiss.inheritancedemo;

public abstract class GreatGrandParent {
	
   public abstract void doStuff();
   
   public void handle(){
	   System.out.println("GreatGrandParent.handle");
	   doStuff();
   }
	
}

   Parent.java


package preiss.inheritancedemo;

public class Parent extends GrandParent{
	
	    @Override
		public void doStuff(){
			System.out.println("Parent.doStuff");
			
			super.doStuff();
		}
		
}

Parent.java hosted on GitHub

   InheritanceDemo.java


package preiss.inheritancedemo;

public class InheritanceDemo {

	public static void main(String[] args) {
		Child cw = new Child();
		cw.handle();
	}
}

Pin It on Pinterest

Share This