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

Sharing is caring.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Like This Post?

We have a lot more where that came from?

We only send really good stuff occasionally, promise.

Rajkumar

Leave a Comment

API Testing eBook

DOWNLOAD FOR FREE