Method Overriding and Run Time Polymorphism

Method Overriding

When you create a method in a subclass with the same signature (name, return type, parameters, types) as that of superclass then the subclass has overridden a method in the superclass. This overridden subclass method hides the corresponding method in the superclass. Whenever you call this method from subclass object, an overridden method in subclass gets called.

For example, consider following the program, where we have created a superclass X and it is having a show() method which prints its data members a and b. A subclass Y derived from superclass X and it overrides show function of superclass X. When we create a new object to class Y and call method show(), what happens? show method defined in subclass gets called. This way we can perform method overriding.

And to call superclass’s show method then we have to use super keyword as shown below.

/*
This is a simple Java program about Inheritance.
Call this file KH_MethodOverriding.java.
*/
class X {
	void show(){
		System.out.println("Simple show in class X");
	}	
	void display(){
		System.out.println("Simple display in class X");
	}
}

class Y extends X {
	void show(){
		System.out.println("Simple Show in class Y");
	}
	void display(){
		System.out.println("Simple Display in class Y");
		super.display();
	}	
}

class KH_MethodOverriding {
	public static void main(String args[]) {
		Y y = new Y();
		y.show(); //only executes class of Y
		y.display();//It executes method in both x and y
	}
}

Download the code  Run the code

Output:

Simple Show in class Y
Simple Display in class Y
Simple display in class X

Run Time Polymorphism

Method overriding is one of the most powerful concepts of Java which provides dynamic method dispatch. Dynamic method dispatch is how an overridden method call is resolved at run-time rather than compile time. This is Java’s run-time polymorphism.

Generally, any call to a method is resolved at compile time, that is which method to call and who owns that method etc are decided at compile time only. But in the case of method overriding, this will be decided when the program is executing. Prior to execution, we do not know which method gets called.

We know that a superclass reference can refer to a subclass object. Java uses this feature to call overridden methods at execution time. When calling overridden methods Java first checks the reference by which this method is getting called at run-time. Based on this reference it decides whether to call superclass method or subclass method.

To understand more, let us look into below program which is an extension of above program, where we have to create two object y1 and y2 for subclass Y and an object x to superclass X. And also defined a reference object ref of type superclass X. Now we will assign ref with object x and call show method. In the same way, we will do for remaining 2 objects y1 and y2. From the output it is clear that when the ref is referencing x of type superclass X, it calls show method of superclass X. And in other cases, it calls to show a method of subclass Y. This is how run-time polymorphism achieved in Java.

/*
This is a simple Java program about Inheritance.
Call this file KH_RuntimePolymorphism.java.
*/
class X {
	void show(){
		System.out.println("Simple show in class X");
	}	
	void display(){
		System.out.println("Simple display in class X");
	}
}

class Y extends X {
	void show(){
		System.out.println("Simple Show in class Y");
	}
	void display(){
		System.out.println("Simple Display in class Y");
		super.display();
	}	
}

class KH_RuntimePolymorphism {
	public static void main(String args[]) {
		Y y1 = new Y();
		Y y2 = new Y();		
		X x1 = new X();
		X x;
		
		x = y1;
		x.show();
		
		x = x1;
		x.show();
		
		x = y2;
		x.show();
	}
}

Download the code  Run the code

Output:

Simple Show in class Y
Simple show in class X
Simple Show in class Y
<<< Access superclass constructor and superclass membersMultilevel Inheritance and Referencing >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .