Case 1: By Method Name
package com.tutorial4u;
class Animal{
public void display() {
System.out.println("I am a animal.");
}
}
class Cat extends Animal {
@Override
public void display()
{
System.out.println("I am a cat.");
}
}
public class OverridingExample {
public static void main(String[] args) {
Animal animal = new Animal();
animal.display(); //Output : From Super Class
Cat cat = new Cat();
cat.display();
//Output : From Sub Class
Animal animal1 = new Cat();
animal1.display();// Output from sub class
}
}
Output :
I am an animal.
I am a cat.
I am a cat.
Case 2: Access Modifier
(A) Increasing visibility → Allowed
package com.tutorial4u;
class Animal{
protected void display() {
System.out.println("I am a animal.");
}
}
class Cat extends Animal {
public void display() {
System.out.println("I am a cat.");
}
}
public class OverridingExample {
public static void main(String[] args) {
Animal animal = new Animal();
animal.display(); //Output : From Super Class
Cat cat = new Cat();
cat.display(); //Output : From Sub Class
}
}
Output :
I am an animal.
I am a cat..
(B)Reducing visibility → Compilation Error
package com.tutorial4u;
class Animal{
protected void display() {
System.out.println("I am a animal.");
}
}
class Cat extends Animal {
//Compilation Error, Cannot reduce the visibility of the inherited method from Animal
//here visibility must be protected or public but not private or default.
private void display() {
System.out.println("I am a cat.");
}
}
public class OverridingExample {
public static void main(String[] args) {
Animal animal = new Animal();
animal.display();
Cat cat = new Cat();
cat.display();
}
}
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 Animal
Case 3: Changing the Number of Parameters (NOT Overriding)
package com.tutorial4u;
class Animal{
public void display() {
System.out.println("I am a animal.");
}
}
class Cat extends Animal {
// This is Overloading, not Overriding
public void display(int i) {
System.out.println(i);
}
}
public class OverridingExample {
public static void main(String[] args) {
Animal animal = new Animal();
animal.display(); //Output : From Super Class
Cat cat = new Cat();
cat.display(); //Output : From super class
cat.display(10);
}
}
Output :
I am an animal.
I am an animal.
10
Note : 👉 Changing the number or type of parameters creates Overloading, not Overriding.
You must not change the arguments of a method in the subclass. If you change the number or types of arguments of the overridden method in the subclass, then it will result in method overloading, not method overriding. ✅
Case 4: Return Type
You can only return same type or a subclass of the parents return type , it is known as covariant return type.
Example :
package com.tutorial4u;
class A {
}
class B extends A {
}
class Super {
public A m() {
System.out.println("Super class method");
return new A();
}
}
class Children extends Super {
public B m() {
System.out.println("Children class method");
return new B();
}
}
public class OverridingExample {
public static void main(String args[]) {
Super sup = new Super();
sup.m();
Children child = new Children();
child.m();
}
}
Output :
Super class method
Child class method
Special Case: Can We Override Static Methods?
-
❌ No. Static methods cannot be overridden.
- ✅ A static method in a subclass with the same signature is considered method hiding, not overriding.
No, but we can declare a static method with the same signature in the subclass. It is not considered overriding, as there will be no runtime polymorphism. In this case, the method in the subclass hides the method in the superclass. ✅
Example 1: Static Method Hiding
package com.tutorial4u;
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();
}
}
Output :
Note : 👉 If m1()
was non-static, the output would be "method B"
(runtime polymorphism).
Example 2:
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.