Method overriding is a feature in Java that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. The method in the subclass must have the same name, return type, and parameters (i.e., the same method signature) as the method in the superclass.
Key Points of Method Overriding:
- Same Method Signature: The method in the subclass should have the same name, return type, and parameter list as the method in the superclass.
- Inheritance: Overriding happens when a subclass inherits from a superclass and modifies an inherited method.
- Runtime Polymorphism: Method overriding is a key feature of runtime polymorphism in Java, meaning that the method that is called is determined at runtime based on the object type (not the reference type).
@Override
Annotation: It is not mandatory, but it is a good practice to use the@Override
annotation to indicate that a method is overriding a method in the superclass. This helps in catching errors like mismatched method signatures.
Syntax of Method Overriding
class Parent {
// Method in the superclass
void display() {
System.out.println("Display from Parent");
}
}
class Child extends Parent {
// Overriding the method in the subclass
@Override
void display() {
System.out.println("Display from Child");
}
}
public class Main {
public static void main(String[] args) {
Parent p = new Parent();
p.display(); // Output: Display from Parent
Child c = new Child();
c.display(); // Output: Display from Child
}
}
Explanation of the Example:
- Parent Class: The
Parent
class has a methoddisplay()
that prints"Display from Parent"
. - Child Class: The
Child
class extends theParent
class and overrides thedisplay()
method to print"Display from Child"
. - Output:
- When we call
p.display()
(wherep
is aParent
object), the method from theParent
class is executed. - When we call
c.display()
(wherec
is aChild
object), the method from theChild
class is executed.
- When we call
Rules for Method Overriding in Java
- Same Method Signature: The method in the subclass must have the same name, return type, and parameter list as the superclass method.
- Access Modifier: The access level of the overriding method cannot be more restrictive than the method in the superclass. For example, if the superclass method is
public
, the subclass method must bepublic
as well. - Return Type: The return type of the overriding method can be the same as the superclass method or a subclass type (covariant return type).
- Exceptions: The overriding method can throw fewer or the same exceptions as the method in the superclass, but not more or different exceptions.
Example: Overriding with Return Type and Exceptions
class Animal {
// Method in the superclass
public void sound() throws Exception {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
// Overriding method in subclass
@Override
public void sound() throws Exception {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
try {
myDog.sound(); // Output: Dog barks
} catch (Exception e) {
e.printStackTrace();
}
}
}
Explanation:
- Superclass
Animal
has asound()
method. - Subclass
Dog
overridessound()
to print"Dog barks"
. - The
sound()
method in both classes has the same signature, andDog
can throw the same type of exception (or fewer exceptions).
Why Use Method Overriding?
- Dynamic Method Dispatch: Method overriding supports runtime polymorphism, where the method called is determined at runtime based on the object type.
- Customization: It allows a subclass to customize or extend the behavior of a method inherited from a superclass without changing the superclass code.
- Code Reusability: By overriding methods, subclasses can reuse the logic in the superclass and add their own specific logic.
Method Overriding vs. Method Overloading
- Method Overriding:
- Occurs when a subclass provides a specific implementation of a method inherited from a superclass.
- It is based on the runtime polymorphism.
- Method signature must be the same.
- Method Overloading:
- Occurs when two or more methods in the same class have the same name but different parameter lists.
- It is based on compile-time polymorphism.
- Method signature must be different (parameters vary).
Conclusion
Method overriding in Java is a powerful feature that allows subclasses to provide specific implementations of methods inherited from superclasses. This promotes polymorphism, flexibility, and code reusability in object-oriented programming. By overriding methods, you can customize behavior in subclasses while still maintaining the structure and behavior defined by the superclass.