Overriding Methods in java

0
698




Overriding Methods

In addition to producing a new class based on an old one by adding additional features, you can modify existing behavior of the parent class. If a method is defined in a subclass so that the name, return type, and argument list match exactly those of a method in the parent class, then the new method is said to override the old one.

Note – In J2SE version 5.0, these matching rules have changed slightly. The return type of the overriding method can now be a subclass of the inherited method. A discussion of covariant returns is beyond the scope of this course.

Consider these sample methods in the Employee and Manager classes:

public class Employee {

protected String name;

protected double salary;

protected Date birthDate;

public String getDetails() {

return “Name: “ + name + “\n”

+ “Salary: “ + salary;

}

}

public class Manager extends Employee {

protected String department;

public String getDetails() {

return “Name: “ + name + “\n”

+ “Salary: “ + salary + “\n”

+ “Manager of: “ + department;

}

}

The Manager class has a getDetails method by definition because it inherits one from the Employee class. However, the original method has been replaced, or overridden, by the child class’s version.



Overridden Methods Cannot Be Less Accessible

Remember that the method name and the order of arguments of a child method must be identical to those of the method in the parent class for that method to override the parent’s version. Furthermore, an overriding method cannot be less accessible than the method it overrides.

Consider this invalid scenario:

public class Parent {

public void doSomething() {}

}

public class Child extends Parent {

private void doSomething() {} // illegal

}

public class UseBoth {

public void doOtherThing() {

Parent p1 = new Parent();

Parent p2 = new Child();

p1.doSomething();

p2.doSomething();

}

}

The Java programming language semantics dictate that p2.method() results in the Child version of method being executed, but, because the
method is declared private, p2 (declared as Parent) cannot access it. Thus, the language semantics are violated.

Invoking Overridden Methods

A subclass method can invoke a superclass method using the super keyword. The super keyword refers to the superclass of the class in which the keyword is used. It is used to refer to the member variables or the methods of the superclass.

This can be achieved using the keyword super as follows:

public class Employee {

private String name;

private double salary;

private Date birthDate;

public String getDetails() {

return “Name: ” + name + “\nSalary: ” + salary;

}

}

public class Manager extends Employee {

private String department;

public String getDetails() {

// call parent method

return super.getDetails()

+ “\nDepartment: ” + department;

}

}

A call of the form super.method() invokes the entire behavior, along with any side effects of the method that would have been invoked if the object had been of the parent class type. The method does not have to be defined in that parent class; it could be inherited from some class even further up the hierarchy.

Note – In the previous example, member variables have been declared as private. This is not necessary but is a generally good programming practice.