Method overriding is a concept in object-oriented programming (OOP) where a subclass provides a specific implementation of a method that is already defined in its superclass. The overriding method in the subclass should have the same name, return type, and parameter list as the method in the superclass. It allows a subclass to modify or extend the behavior of a method inherited from the parent class.
Key Points about Method Overriding:
- Same Method Signature: The method in the subclass must have the same name, return type, and parameters as the method in the superclass.
- Access Modifier: The access level of the overriding method in the subclass cannot be more restrictive than the method in the superclass. For example, if the method in the superclass is
public
, the method in the subclass must also bepublic
orprotected
, but notprivate
. - Polymorphism: Method overriding is a key feature of polymorphism. It allows a subclass object to be treated as an instance of its superclass, but the overridden method of the subclass will be invoked when called on the subclass object.
@Override
Annotation: In Java, the@Override
annotation is used to indicate that a method is being overridden. This is not required but helps the compiler check if the method is indeed overriding a superclass method and not accidentally defining a new method.
Syntax:
@Override
public returnType methodName(parameters) {
// Method body
}
Code language: PHP (php)
@Override
: This annotation tells the compiler that you are overriding a method from the superclass.- returnType: Must be the same as the superclass method or a subtype (covariant return types are allowed).
- methodName: Must be the same as in the superclass.
- parameters: Must have the same parameter list as the method in the superclass.
Program
class Employee { String name; int employeeId; // Constructor to initialize Employee details public Employee(String name, int employeeId) { this.name = name; this.employeeId = employeeId; } // Method to display employee details public void showDetails() { System.out.println("Employee Name: " + name); System.out.println("Employee ID: " + employeeId); } // Method to calculate salary public void calculateSalary() { System.out.println("Salary: $50,000"); } } class SoftwareEngineer extends Employee { String programmingLanguage; // Constructor to initialize Software Engineer details public SoftwareEngineer(String name, int employeeId, String programmingLanguage) { super(name, employeeId); this.programmingLanguage = programmingLanguage; } // Overriding the showDetails method @Override public void showDetails() { super.showDetails(); // Call the superclass method System.out.println("Programming Language: " + programmingLanguage); } // Overriding the calculateSalary method @Override public void calculateSalary() { System.out.println("Salary: $120,000"); } } public class Main { public static void main(String[] args) { // Create an Employee object Employee employee = new Employee("Paani", 101); employee.showDetails(); employee.calculateSalary(); // Create a SoftwareEngineer object SoftwareEngineer engineer = new SoftwareEngineer("Mahesh", 102, "Java"); engineer.showDetails(); engineer.calculateSalary(); } } /* Employee Name: Paani Employee ID: 101 Salary: $50,000 Employee Name: Maheh Employee ID: 102 Programming Language: Java Salary: $120,000 */
Method overriding is a fundamental concept in object-oriented programming, especially in Java, which allows a subclass to provide a specific implementation for a method already defined in its superclass. It plays a vital role in polymorphism, allowing for more flexible and dynamic behavior when working with objects.