3-๐Ÿ” Method Overriding in Java โ€“ Complete Guide with Examples, Rules & Common Mistakes

๐ŸŒŸ Introduction

In the previous articles of this series, we learned:

  • What inheritance is
  • Different types of inheritance in Java

Now comes one of the most important and powerful concepts in Java OOP:

๐Ÿ‘‰ Method Overriding

Up to now, we saw that a child class can inherit methods from a parent class.
But what if the child class wants to change the behavior of that method?

For example:

  • All animals make sound
  • But dogs bark, cats meow

So the behavior is different, even though the method is the same.

This is exactly what method overriding solves.


๐Ÿง  What is Method Overriding?

Method overriding occurs when:

A child class provides its own implementation of a method that is already defined in the parent class.

In simple words:

๐Ÿ‘‰ Child class replaces parent class method behavior.


๐Ÿงฉ Real-Life Analogy

Think of a parent class = Animal

It has a method:

makeSound()

Now:

  • Dog โ†’ barks
  • Cat โ†’ meows

Same method name โ†’ different behavior

๐Ÿ‘‰ This is method overriding.


๐Ÿ’ป Basic Example of Method Overriding

class Animal {

    void sound() {
        System.out.println("Animal makes sound");
    }

}

class Dog extends Animal {

    void sound() {
        System.out.println("Dog barks");
    }

}

public class Main {

    public static void main(String[] args) {

        Dog d = new Dog();
        d.sound();

    }

}

๐Ÿ”Ž Line-by-Line Explanation

Parent class:

class Animal

This is the base class.


Method in parent:

void sound()

This method defines a general behavior.


Child class:

class Dog extends Animal

Dog inherits from Animal.


Overridden method:

void sound()

Same method name as parent.

๐Ÿ‘‰ This is method overriding.


New behavior:

System.out.println("Dog barks");

Dog changes the behavior of sound().


๐Ÿงพ Output

Dog barks

๐Ÿ‘‰ Parent method is not executed, child version runs.


โš™๏ธ Important Rules of Method Overriding


โœ” Rule 1: Method Name Must Be Same

void sound()

If the name changes โ†’ it is NOT overriding.


โœ” Rule 2: Parameters Must Be Same

void sound()

If parameters differ โ†’ it becomes method overloading, not overriding.


โœ” Rule 3: Return Type Must Be Same (or compatible)

int getValue()

Return type must match or be covariant.


โœ” Rule 4: Access Modifier Rules

Child class method cannot be more restrictive.

ParentChild Allowed
publicpublic
protectedprotected / public

โœ” Rule 5: Cannot Override Certain Methods

โŒ final methods
โŒ static methods
โŒ private methods


๐Ÿš€ Runtime Polymorphism (Most Important Concept)

This is where method overriding becomes powerful.


๐Ÿ’ป Example

class Animal {

    void sound() {
        System.out.println("Animal makes sound");
    }

}

class Dog extends Animal {

    void sound() {
        System.out.println("Dog barks");
    }

}

public class Main {

    public static void main(String[] args) {

        Animal a = new Dog();
        a.sound();

    }

}

๐Ÿ”Ž Explanation

Animal a = new Dog();

๐Ÿ‘‰ Reference type = Animal
๐Ÿ‘‰ Object type = Dog


a.sound();

Which method will run?

๐Ÿ‘‰ Output:

Dog barks

๐Ÿง  Why?

Because Java decides the method at runtime, not compile time.

This is called:

๐Ÿ‘‰ Runtime Polymorphism


๐Ÿท Using @Override Annotation

@Override
void sound() {
System.out.println("Dog barks");
}

โœ… Why Use It?

โœ” Avoids mistakes
โœ” Ensures method is actually overridden
โœ” Improves readability


โš ๏ธ If Method is Wrong

@Override
void sounds() { }

โŒ Compiler error
๐Ÿ‘‰ Helps catch mistakes early


๐Ÿ”„ Method Overriding vs Method Overloading

FeatureOverridingOverloading
ClassDifferent classesSame class
Method nameSameSame
ParametersSameDifferent
PurposeChange behaviorMultiple versions

๐Ÿงจ Tricky Interview Example

class A {

    void show() {
        System.out.println("Class A");
    }

}

class B extends A {

    void show() {
        System.out.println("Class B");
    }

}

public class Main {

    public static void main(String[] args) {

        A obj = new B();
        obj.show();

    }

}

๐Ÿง  Output

Class B

๐Ÿ‘‰ Even though reference is A, object is B.


โš ๏ธ Common Mistakes & Confusions


โŒ Mistake 1: Changing Parameters

void sound(int x)

โŒ Not overriding
โœ” Overloading


โŒ Mistake 2: Static Methods

static void show()

โŒ Static methods are not overridden
โœ” They are hidden


โŒ Mistake 3: Private Methods

private void show()

โŒ Not inherited โ†’ cannot override


โŒ Mistake 4: Thinking Compile-Time Decision

Many students think:

Method is chosen based on reference type

โŒ Wrong

๐Ÿ‘‰ It is chosen based on object type at runtime


โŒ Mistake 5: Forgetting @Override

Leads to silent bugs.


๐ŸŽฏ Real-Life Example

class Vehicle {

    void start() {
        System.out.println("Vehicle starts");
    }

}

class Car extends Vehicle {

    @Override
    void start() {
        System.out.println("Car starts with key");
    }

}

class Bike extends Vehicle {

    @Override
    void start() {
        System.out.println("Bike starts with kick");
    }

}

๐Ÿง  Key Points to Remember

โœ” Method overriding happens in inheritance
โœ” Same method โ†’ different behavior
โœ” Achieved at runtime
โœ” Enables polymorphism
โœ” @Override is recommended
โœ” Cannot override final, static, private


๐Ÿ Conclusion

Method overriding is one of the most powerful features of Java, allowing developers to create programs that are flexible, dynamic, and scalable.

It enables a child class to customize or replace the behavior of a parent class method, making programs more realistic and adaptable.

In this article, we covered:

  • What method overriding is
  • How it works
  • Rules of overriding
  • Runtime polymorphism
  • @Override annotation
  • Common mistakes and confusions

Understanding method overriding is crucial because it forms the foundation of runtime polymorphism, which is heavily used in real-world applications and interviews.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *