Method overriding in java

If subclass is having same method as base class then it is known as method overriding Or in another words, If subclass provides specific implementation to any method which is present in its one of parents classes then it is known as method overriding
Lets start with a real time example:

In a small organization,There are two kinds of Employees i.e. Manager and Developer. Now we want to print salary of employee.

Create class in org.arpit.java2blog


Run it:
When you will run will get following output:

Name of Employee:Arpit—Salary:22000.0
Name of Employee:John—Salary:16500.0
Name of Employee:Amit—Salary:36000.0
Name of Employee:Ashwin—Salary:60000.0

As you can see here, We are overriding getSalary() method of Employee class in Developer and Manager. Why we need to override getSalary()?
Because we require specific implementation of getSalary() method based on Class. e.g. Developer class and Manager class have different bonus so we need different implementation for both.

Rules for method overriding

Arguments Must not change
Return type Can’t change except for covariant (subtype) returns
Access Modifier Must not be more restrictive. Can be less restrictive.
Exceptions Can reduce or eliminate but must not throw new/broader checked exceptions
Contructor Can not be overriden
Static method Can not be overriden
final method Can not be overriden

Now here, I will answer some of the obvious question you could have:


Why can’t you make access modifier more restrictive (e.g. public to private)?

It’s a fundamental principle in OOP: the child class is a fully-fledged instance of the parent class, and must therefore have at least the same interface as the parent class. Making less visible would violate this idea; you could make child classes unusable as instances of the parent class.
Lets see with the help of example:

Now I could call using :

so even you made getSalary() method of developer private, you will be able to access getSalary() method of developer using reference of Employee(super class) even if you have made it private. so you can’t reduce access modifier while overriding method.

Why can’t you override static methods?

Because static methods are related to class not to state of object so You can declare static method in child class but it has nothing to do with parent class. It is method hiding not overriding.

Can you override private methods?

No,you can’t. A private method cannot be overridden since it is not visible from any other class. You can declare a new method for your subclass that has no relation to the superclass method. So it is not method overriding.

Why can’t you override final methods?

Because final methods are meant to be not overridden.You declare a method final because you don’t want it to be overridden in subclass.

What if We change number of arguments?

If you change number of arguments then it will be method overloading not overriding. Parent class method and child class method should have same method signature.

What is dynamic binding?

Binding of overridden methods happen at runtime is known as dynamic binding.

Super keyword

Super keyword can be used to to call specific parent class method from child class.
For example:
When you run the program, you will get following output:

That’s all about Method Overriding in java.

Was this post helpful?


Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.