Updated June 21, 2023
Introduction to Method Overriding in Java
It is a feature that allows the child class or subclass to redefine or provide the specific implementation for the method already defined by one of the superclasses. The subclass method redefined in the subclass is called method overriding in java. Method overriding requires the child class to have a method with the same name, parameter list, and return type as a method in its parent class. In other words, they should share the same method signature. If these conditions meet, we say that the method in the child class overrides the method from its parent class.
The Method overriding feature is used to perform the dynamic polymorphism in java. Another usage of Method overriding is to provide the specific implementation for the child class method, which is already provided in the parent class. We can understand the method overriding more clearly with the help of the below diagram.
As in the above example figure, the codePermanentEmp and TemporaryEmp classes inherit the class Employee. Both the codePermanentEmp and TemporaryEmp classes can reuse the Employee variables and methods. However, each class redefines the incrementSalary() method to calculate the salary increment according to the employee type. So both the classes override the incrementSalary() method. The subclass and the employee class must have the same method name, parameter list, and return type to meet the conditions for method overriding.
Syntax of Method Overriding in Java
Let’s see the syntax of method overriding in java:
class Superclassname
{
// variables
void methodA()
{
// method implementation code
}
}
class Subclassname1 extends Superclassname
{
// variables
void methodA()
{
// method specific implementation or re-implemented code
}
}
class Subclassname2 extends Superclassname
{
// variables
void methodA()
{
// method specific implementation or re-implemented code
}
}
The keyword ‘extended’ signifies creating a new class derived from an existing one. Therefore, the methodA(), initially defined in the superclass, gets redefined in the subclasses.
The essential points to remember about the override method are:
- The overridden method allows more or equal, but not less, access modifiers to override the method.
- Private, Final, and static methods can not be overridden.
- Abstract methods must override the subclass of an interface or abstract class; otherwise, a compile-time error will be thrown.
Examples of Method Overriding in Java
Below are the example of method overriding in java to override a method of superclass –
Example #1
Next, we write the java code to understand the method overriding in java to override a method of the superclass with the following example –
Code:
class Employee{
float salary = 40000;
void incrementSalary()
{
System.out.println("The Employee incremented salary is :" +(salary + (salary * 0.2)) );
}
}
class PermanentEmp extends Employee{
double hike = 0.5;
void incrementSalary()
{
System.out.println("The Permanent Employee incremented salary is :" +(salary + (salary * hike)) );
}
}
class TemporaryEmp extends Employee{
double hike = 0.35;
void incrementSalary()
{
System.out.println("The Temporary Employee incremented salary is :" +(salary + (salary * hike)) );
}
}
public class p1
{
public static void main(String args[]){
Employee e =new Employee( );
PermanentEmp p = new PermanentEmp();
TemporaryEmp t = new TemporaryEmp();
// based on an object it decide which class incrementSalary() method to be execute
e.incrementSalary();
p.incrementSalary();
t.incrementSalary();
}
}
Output:
As in the above code, PermanentEmp class and TemporaryEmp classes are the subclasses, and employee is the superclass inside subclasses; the method incrementSalary() is overriding from the superclass, as we can see in the code, the override method incrementSalary() has the same name, same signature and same return type as in the superclass Employee. In the primary method, we create objects of classes and determine which class’s incrementSalary() method to execute based on the object itself. This decision relies on dynamic polymorphism or late binding; it performs the same class method as the object belongs, which we can see clearly in the output.
Example #2
Let’s see an example of a method overriding in java to override the parameterized method of the superclass.
Code:
class Employee{
float salary = 40000;
void incrementSalary(double hike)
{
System.out.println("The Employee incremented salary is :" +(salary + (salary * hike)) );
}
}
class PermanentEmp extends Employee{
void incrementSalary(double hike)
{
System.out.println("The Permanent Employee incremented salary is :" +(salary + (salary * hike)) );
}
}
class TemporaryEmp extends Employee{
void incrementSalary(double hike)
{
System.out.println("The Temporary Employee incremented salary is :" +(salary + (salary * hike)) );
}
}
public class p1
{
public static void main(String args[]){
Employee e =new Employee( );
PermanentEmp p = new PermanentEmp();
TemporaryEmp t = new TemporaryEmp();
// based on an object it decide which class incrementSalary() method to be execute
e.incrementSalary(0.2);
p.incrementSalary(0.5);
t.incrementSalary(0.35);
}
}
Output:
In the above example, the PermanentEmp and TemporaryEmp classes override the incrementSalary(double hike ) method.
Conclusion
Method override in java is a feature in which one superclass method redefines or again implements the subclass. To override a method, a child class must have the same name, the same parameters list or same signature, and the same return type as a method in its parent class; in this case, we use ‘temp_num’ to compare the result with the original number.
Recommended Articles
This is a guide to Method Overriding in Java. Here we discuss the introduction, syntax, and various examples of Method Overriding in Java. You can also go through our other related articles to learn more –