In this post of Method Overriding or Dynamic Polymorphism in Java you will learn about Method Overriding or Dynamic Polymorphism in detail.
In a parent-child relation between classes, if a method in sub class has same signature as the parent class method then the method is said to be overridden by the sub class and this kind of process is called method overriding in Java.
Example of Method overriding in Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.infotech.model; public class ParentClass { private int i; // Constructor with one parameter public ParentClass(int i) { this.i = i; } // Method with no parameter public void dispayValue() { System.out.println("Value of i " + i); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.infotech.model; public class ChildClass extends ParentClass { private int j; // Constructor with two parameters public ChildClass(int i, int j) { // Calling parent or super class constructor super(i); this.j = j; } // Overridden Method public void dispayValue() { System.out.println("Value of j " + j); } } |
1 2 3 4 5 6 7 8 9 10 11 |
package com.infotech.client; import com.infotech.model.ChildClass; public class ClientOverridingTest { public static void main(String[] args) { ChildClass childClass = new ChildClass(20, 100); childClass.dispayValue(); } } |
Output of the program:
1 |
Value of j 100 |
It can be noticed here that when displayValue() is invoked on an object of class ChildClass, the overridden displayValue() method of the child class is called.
You must note that in the case of parent-child relationship if both classes have the method with same name and signature then only it is known as method overriding, otherwise it is considered as method overloading only.
Benefit of method overriding in Java
Method overriding allows java to support run-time polymorphism which in turn helps in writing more robust code and code reuse.
Method overriding helps us in hierarchical ordering where we can move from general to specific.
If you use the same above example to demonstrate run time Polymorphism here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
package com.infotech.model; public class ParentClass { private int i; // Constructor with one parameter public ParentClass(int i) { this.i = i; } // Method with no parameter public void dispayValue() { System.out.println("Value of i " + i); } } package com.infotech.model; public class ChildClass extends ParentClass { private int j; // Constructor with two parameters public ChildClass(int i, int j) { // Calling parent or super class constructor super(i); this.j = j; } // Overridden Method public void dispayValue() { System.out.println("Value of j " + j); } } package com.infotech.client; import com.infotech.model.ChildClass; import com.infotech.model.ParentClass; public class ClientOverridingTest { public static void main(String[] args) { ChildClass childClass = new ChildClass(20, 100); ParentClass parentClass = new ParentClass(190); //calling parent dispayValue method parentClass.dispayValue(); //parent reference holding the reference of child class parentClass = childClass; /*child class displayValue method will be called Because Here parent class reference holding the reference of child class Object*/ parentClass.dispayValue(); } } |
Output of the program:
1 2 |
Value of i 190 Value of j 100 |
It can be noticed here that initially parent object(object of ParentClass) calls displayValue() method of parent class(ParentClass), later at run time the reference is changed and the parent is assigned the reference of child class(ChildClass). Now, when the displayValue() method is called on the parent object, it calls the overridden method of the class Child(ChildClass).
Some of the key points about method overriding
- If method in the sub-class has the same name and signature as the parent class method then only it is called method overriding otherwise it is method overloading.
- Method overriding allows java to support run-time polymorphism.
- The Method overriding helps us in hierarchical ordering where you can move from general to specific implementation. In super class a method can have a general implementation whereas the sub classes provide the more specific implementation by overriding the parent class method.
- If a method is declared as final in parent class then it can not be overridden by sub classes.
- If a method is declared as abstract in the parent class then the sub class has to provide implementation for those inherited abstract methods
That’s all about Method Overriding or Dynamic Polymorphism in Java
You May Also Like:
Encapsulation in Java
Abstraction in Java
Method Overloading or Static Polymorphism in Java
Understating of Inheritance in Java
Difference Between Encapsulation and Abstraction in Java
Association Aggregation And Composition in Java
If you have any feedback or suggestion please feel free to drop in the blow comment box.