Method Overriding in Java

Method overriding is one of the importance concept in Java Programming.

What is Method Overriding?

In Inheritance hierarchy when a child class defines exact same method that is available in parent class then the method is called overridden method.

The process of creating overridden method is called method overriding.

Result

We have created a class Shape.

Also created child classes Triangle, Rectangle and Circle.

On child class we have created same method draw(). Creating exact same method in child is known as method overriding.

In child class method signature and return type is exact same as parent but child class provides new definition to overridden method.

Why method overriding?

When defined method in parent class is not suitable for child class then we override method.

As in above example method draw() is overridden in all child class.

Because draw() behaves differently for different classes.

Note: If we want to access the super class overridden method, we can do so by using super keyword.

Note: Overriding occurs only when the method name and parameter list of the method are identical.  

Method with same name and different parameter list are simply overloaded not overridden.

Result