Method Overriding In Java
Method Overriding: (Dynamic Polymorphism/Run time polymorphism)
Declaring a method in child class which is already present in the parent class is called Method Overriding.
In simple words, overriding means to override the functionality of an existing method.
In this case, if we call the method with child class object, then the child class method is called. To call the parent class method we have to use super keyword.
Let’s see a practical example to understand well.
package methodOverriding; public class MethodOverridingParentClass { public void myMethod(){ System.out.println("I am a method from Parent Class"); } }
package methodOverriding; public class MethodOverridingChildClass extends MethodOverridingParentClass{ public void myMethod(){ System.out.println("I am a method from Child Class"); } public static void main(String [] args){ MethodOverridingChildClass obj = new MethodOverridingChildClass(); // It calls the child class method myMethod() obj.myMethod(); } }
Output:
I am a method from Child Class
With method overriding a child class can give its own specific implementation to an inherited method without modifying the parent class method. If a child class can give its own implementation then it helps
Assume we have multiple child classes. In case one of the child classes want to use the parent class method and other class want to use their own implementation then we can use overriding feature.
Method overriding is also known as runtime polymorphism. Let’s see why we call it as runtime polymorphism.
ParentClass obj = new ChildClass();
When a parent class reference refers to the child class object then the call to the overridden method is determined at the runtime. So it is called runtime polymorphism. It is because during method call which method (parent class or child class) is to be executed is determined by the type of an object.
Type of an object determines which method (either parent class or child class) is to be executed during method call.
Lets see an example to understand clearly.
package methodOverriding; public class MethodOverridingParentClass { public void myMethod(){ System.out.println("I am a method from Parent Class"); } }
package methodOverriding; public class MethodOverridingChildClass extends MethodOverridingParentClass{ public void myMethod(){ System.out.println("I am a method from Child Class"); } public static void main(String [] args){ /* When parent class reference refers to the child class object * then the method of the child class (overriding method) is called. * This we call as runtime polymorphism */ MethodOverridingParentClass obj = new MethodOverridingChildClass(); // It calls the child class method myMethod() obj.myMethod(); /* When Parent class reference refers to the parent class object * then the method of parent class (overriden method) is called. */ MethodOverridingParentClass obj1 = new MethodOverridingParentClass(); obj1.myMethod(); } }
Output:
I am a method from Child Class I am a method from Parent Class
Must Read: Java Tutorial