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
Table of Contents
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 Employee.java in org.arpit.java2blog
Employee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package org.arpit.java2blog; public class Employee { Â Â Â int employeeId; Â Â Â String employeeName; Â Â Â double salary; Â Â Â public Employee(int employeeId, String employeeName, double salary) { Â Â Â Â Â Â super(); Â Â Â Â Â this.employeeId = employeeId; Â Â Â Â Â Â this.employeeName = employeeName; Â Â Â Â Â Â this.salary = salary; Â Â Â } Â Â Â public int getEmployeeId() { Â Â Â Â Â Â return employeeId; Â Â Â } Â Â Â public void setEmployeeId(int employeeId) { Â Â Â Â Â Â this.employeeId = employeeId; Â Â Â } Â Â Â public String getEmployeeName() { Â Â Â Â Â Â return employeeName; Â Â Â } Â Â Â public void setEmployeeName(String employeeName) { Â Â Â Â Â Â this.employeeName = employeeName; Â Â Â } Â Â Â public double getSalary() { Â Â Â Â Â Â return salary; Â Â Â } Â Â Â public void setSalary(double salary) { Â Â Â Â Â this.salary = salary; Â Â Â } } |
Manager.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package org.arpit.java2blog; public class Manager extends Employee{ Â Â Â public static final double BONUSPERCENT=0.2; Â Â Â public Manager(int employeeId, String employeeName, double salary) { Â Â Â Â Â Â super(employeeId, employeeName, salary); Â Â Â } Â Â Â public double getSalary() { Â Â Â Â Â Â return salary+salary*BONUSPERCENT; Â Â Â } } |
Developer.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package org.arpit.java2blog; public class Developer extends Employee{Â Â Â Â public static final double BONUSPERCENT=0.1; Â Â Â public Developer(int employeeId, String employeeName, double salary) { Â Â Â Â Â Â super(employeeId, employeeName, salary);Â Â Â Â Â Â Â Â Â } Â Â Â public double getSalary() { Â Â Â Â Â Â return salary+salary*BONUSPERCENT; Â Â Â } } |
MethodOverridingMain:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package org.arpit.java2blog; public class MethodOverridingMain {    /**     * @author Arpit Mandliya     */    public static void main(String[] args) {       Developer d1=new Developer(1,"Arpit" ,20000);       Developer d2=new Developer(2,"John" ,15000);       Manager m1=new Manager(1,"Amit" ,30000);       Manager m2=new Manager(2,"Ashwin" ,50000);       System.out.println("Name of Employee:" +d1.getEmployeeName()+"---"+"Salary:"+d1.getSalary());       System.out.println("Name of Employee:" +d2.getEmployeeName()+"---"+"Salary:"+d2.getSalary());       System.out.println("Name of Employee:" +m1.getEmployeeName()+"---"+"Salary:"+m1.getSalary());       System.out.println("Name of Employee:" +m2.getEmployeeName()+"---"+"Salary:"+m2.getSalary());    } } |
Run it:
When you will run MethodOverridingMain.java.You will get following output:
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:
Questions
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Employee{  public double getSalary(){      //some operation    }  } class Developer extends Employee{   private double id getSalary(){       //some operation   }  } |
Now I could call using :
1 2 3 4 |
Employee e1=new Developer(); e1.getSalary(); |
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?
What if We change number of arguments?
What is dynamic binding?
Super keyword
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package org.arpit.java2blog; public class Employee{ public double getSalary(){ System.out.println("In Employee class getSalary() method"); return 0; } public static void main(String args[]) { Developer d1=new Developer(); d1.getSalary(); } } class Developer extends Employee{ public double getSalary(){ // calling parent class method super.getSalary(); System.out.println("In Developer class getSalary() method"); return 0; } } |
1 2 3 4 |
In Employee class getSalary() method In Developer class getSalary() method |