Method Overriding in Java occurs when a subclass provides a specific implementation of a method that is already defined in its superclass, using the same method name and same parameter list (method signature).
- It is used to achieve runtime polymorphism.
- It allows a subclass to provide its own implementation of an inherited method without modifying the parent class code.
Method Name → Must be the same as the superclass method.
-
Parameters → Must have the same number and type of parameters.
-
Access Modifier → Cannot be more restrictive than the superclass method.
Restriction order:
private ⟹ default ⟹ protected ⟹ public
-
Return Type → Must be the same or a covariant return type (subclass of the parent’s return type).
-
Exceptions → Overriding methods can throw unchecked exceptions, but for checked exceptions, the subclass method cannot throw broader or new checked exceptions that the parent method doesn’t declare. Check ๐here in details.
Examples of Method Overriding :
I am an animal. I am a cat. I am a cat.
Case 2: Access Modifier
(A) Increasing visibility → Allowed
I am an animal. I am a cat..
Note: ๐ The subclass cannot override the method with a more restrictive access modifier.
Output :
Compilation Error, Cannot reduce the visibility of the inherited method from AnimalCase 3: Changing the Number of Parameters (NOT Overriding)
package com.tutorial4u;
I am an animal. I am an animal. 10
Super class method
Child class method
- ❌ No. Static methods cannot be overridden.
- ✅ A static method in a subclass with the same signature is considered method hiding, not overriding.
class A {
public static void m1() {
System.out.println("method A");
}
}
class B extends A {
public static void m1() {
System.out.println("method B");
}
}
public class OverridingExample {
public static void main(String args[]) {
A a = new B();
a.m1();
}
}
method A
m1()
was non-static, the output would be "method B"
(runtime polymorphism).package com.tutorial4u;
class A {
public static void m1() {
System.out.println("method a1");
}
public void m2() {
System.out.println("method a2");
}
}
class B extends A {
//Compilation Error: This
instance method cannot override the static method from A
public void m1() {
System.out.println("method b1");
}
//Compilation Error: This
static method cannot hide the instance method from A
public static void m2() {
System.out.println("method b2");
}
}
public class OverridingExample {
public static void main(String args[]) {
A a = new B();
a.m1();
a.m2();
}
}
Output :
Compilation Error
This instance method cannot override the static method from A
This static method cannot hide the instance method from A
Important Points About Method Overriding
- Private methods cannot be overridden (not inherited by subclasses).
- Final methods cannot be overridden(not inherited by subclasses).
- Static methods cannot be overridden (only hidden).
- main() method cannot be overridden (because it’s static).
- An overriding method cannot have a more restrictive access modifier.
- Only instance methods can be overridden.