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 which is already defined by one of the superclasses. In simple words, the superclass method redefines in the subclass is called Method overriding in java. The condition for method overriding is a child class should have the same name, same parameters list or other in words method signature and same return type as a method in its parent class, then the method in the child class is said to override the method of 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 class and TemporaryEmp class inherits the class Employee. So the Employee variables and methods can reuse in both classes, codePermanentEmp class, and TemporaryEmp class, but both the classes PermanentEmp and TemporaryEmp are redefined the method incrementSalary() to calculate the salary increment specific to the type of employee. So both the classes are overriding the incrementSalary() method. According to the conditions of method overriding, they are also satisfied as they have the same method name, same parameters list, and same return type as in the employee class.
Syntax of Method Overriding in Java
Let 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
}
}
An extended keyword indicates that we are making a new class that derives from an existing class, so the method methodA(), which is already defined in the superclass, is redefining in the subclasses.
The important points to remember about the override method are:
- The overridden method allows more or equal, but not less, access modifiers to overriding the method.
- Private, Final, and static methods can not be overridden.
- Subclass of an interface or abstract class must be overridden Abstract methods; 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 super class –
Example #1
Next, we write the java code to understand the method overriding in java to override a method of the super class 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 and inside a subclasses, the method incrementSalary() is overriding from the superclass, as we can see in the code, the override method incrementSalary() have the same name, same signature and same return type as in the superclass Employee. Another important point as in the main method objects of classes are created and calling incrementSalary() method, which class incrementSalary() method is to be executed is decide based on the object itself ( dynamic polymorphism or late binding), it executes the same class method as the object belong, which we can see clearly in the output.
Example #2
Let 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 class and TemporaryEmp class override the incrementSalary(double hike ) method.
Conclusion
Method override in java is a feature in which one superclass method redefines or again implements subclass. To override a method, a child class must have the same name, same parameters list or same signature, and same return type as a method in its parent class, then the method in the child class is said to override the method of its parent class.
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 –
41 Online Courses | 29 Hands-on Projects | 305+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses