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.

Employee.java:
- 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;
- }
- }
- 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;
- }
- }
- 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;
- }
- }
- 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());
- }
- }
When you will run MethodOverridingMain.java.You 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
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 |
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:
- class Employee{
- public double getSalary(){
- //some operation
- }
- }
- class Developer extends Employee{
- private double id getSalary(){
- //some operation
- }
- }
- Employee e1=new Developer();
- e1.getSalary();
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:
- 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;
- }
- }
When you run the program, you will get following output:
- In Employee class getSalary() method
- In Developer class getSalary() method
For more Visit: http://www.quontrasolutions.com/blog/category/java
No comments:
Post a Comment