Java - Method Overriding
Implementation in Java
- Java is a commonly used, high-level, true object-oriented programming language.
- It is known for its platform independence, readability, and versatility.
- It was originally developed by James Gosling and his team at Sun Microsystems.
- It was first released in 1995.
- It supports multithreading, allowing developers to create concurrent and parallel applications easily.
Method Overriding - Class Diagram |
Inheritance permits reusability of code. The completeName() method defined in Person class is inherited in Employee(the Sub Class).
Method Overriding permits to override its functionality in the Sub class.
Method overriding is in fact a feature of Object-Oriented Languages that allows customizing and extending the behavior of classes in a flexible and dynamic manner.
class Parent {
void m1() {
System.out.println("Hello !!! from m1 of the Parent Class.");
}
}
class Child extends Parent {
// The Child class is overriding the m1() from the Parent class
@Override
void m1() {
System.out.println(Hello !!! from m1 of the Child Class.");
}
}
public class Main {
public static void main(String[] args) {
Parent parentObj = new Child();
parentObj.m1(); // Calls the Child's overridden method
}
}
The @Override annotation helps improve code readability and catch errors early during compilation.
The class diagram shown above is implemented in Java. The Person class is defined as follows:
public class Person {
//fields to store First and Last Name of a person
//Declares as protected to make it accessible in Employeee class
protected String firstName;
protected String lastName;
//Constructor to initiliaze fields
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
//Displays name in Firstname and Lastname format
//Can be overridden in sub class
public void completeName() {
System.out.println(firstName + " " + lastName);
}
}
The Employee class is defined by inheriting from the Person class. Java provides extends keyword to inherit a class. The constructor is also defined to pass parameters required by Person class constructor. Java provides super keyword to pass parameters from Employee class(Sub class) to the Person class(Super class) from the constructor of the sub class.
The completeName() method gets inherited from the Person class. It displays the name of the Person in the First and Last name format.
Method Overriding is required when name of the Employee is required in the Last and First name format. It is implemented by redefining completeName() method in the Employee class with the same signature as demonstrated in the code below:
public class Employee extends Person{
//It is compulsory to define constructor in Sub class
//As no default constructor in super class
//Its job is provide parameters required by super class constructor
public Employee(String firstName, String lastName) {
super(firstName, lastName);
//Java provides super keyword to pass parameters to super class constructor
}
//Method inherited from Person is overridden here
//Displays name in Lastname and Firstname format
public void completeName() {
System.out.println(lastName + " " + firstName);
}
}
Once the Person and the Employee classes are defined then can be tested using the following code.
//code to test the defined Person and Employee Class
public class Test {
public static void main(String[] args) {
//Creating an instance or an object
//A reference variable of Super class can refer to
// Objects of Super as well as Sub Class
Person person;//Reference of Person Class
//Refers to object of Person Class
person = new Person("Neeraj","Chopra");
System.out.println("calls Super class version");
person.completeName();
//Refers to object of Employee Class
person = new Employee("Neeraj","Chopra");
System.out.println("calls Sub class version");
person.completeName();
}
}
The output of the above code is:
calls Super class versionNeeraj Chopra
calls Sub class version
Chopra Neeraj
Press any key to continue . . .
Wondering:
- Why all that lengthy code?
- Why not make change in Person class code to get the work done?
Remember in a typical development Scenario, a team of developers working who may be from different organizations. One developer has developed the Person class and shared .jar file with other developers. Source code is not shared therefore code is not available to update it.
What if both the versions of the completeName() is required?
Method Overriding is feature of Object-Oriented Languages to achieve runtime polymorphism. Recall in When Person(Super class) Object refers to the object of the Person class calls the super class version of the completeName() method and when it refers to the object of the Employee class (Sub class) calls the sub class version of the completeName() method.
However, the developed of Person may decide not to allows the users of his/her class to override than the developer can mark completeName() method with the final keyword.
final void completeName();
Java - Method Overriding
Reviewed by Syed Hafiz Choudhary
on
September 17, 2023
Rating:
No comments: