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.

Output:

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.

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.

Output:

Must Read: Java Tutorial

Rajkumar SM is a founder of SoftwareTestingMaterial. He is a certified Software Test Engineer by profession and blogger & youtuber by choice. He has an extensive experience in the field of Software Testing. He writes here about Software Testing which includes both Manual and Automation Testing. He loves to be with his wife and cute little kid 'Freedom'.

Leave a Comment

Share via
Copy link
Powered by Social Snap