Tutorials Home   >   Programming Basics   >   Method Overriding

Method Overriding

1. Introduction to Method Overriding

In object-oriented programming (OOP), classes can inherit properties and methods from other classes. Sometimes, a child class needs to change the behavior of a method that it inherits from its parent class. This concept is known as method overriding.

Method overriding occurs when:

  • A subclass provides its own implementation of a method

  • The method already exists in the parent (super) class

  • Both methods have the same name, return type, and parameters

Method overriding allows a program to show different behavior for the same method, depending on the object that is calling it.


2. Importance of Method Overriding

Method overriding is important because it:

  1. Supports runtime polymorphism

  2. Allows customized behavior in child classes

  3. Makes programs more flexible and extensible

  4. Helps in code reusability

  5. Improves readability and maintainability

Without method overriding, all child classes would behave exactly like the parent class, which is often not practical.


3. Understanding Inheritance

To understand method overriding, we must first understand inheritance.

What Is Inheritance?

Inheritance is a mechanism where:

  • One class (child/subclass) inherits the properties and methods of another class (parent/superclass)

Example

class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
}

Here:

  • Dog inherits the sound() method from Animal


4. What Is Method Overriding?

Method overriding happens when:

  • The subclass redefines a method of the superclass

  • The method signature remains the same

Example of Method Overriding

class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println(“Dog barks”);
}
}

In this example:

  • Dog overrides the sound() method

  • When sound() is called on a Dog object, the child class version runs


5. How Method Overriding Works

Method overriding is resolved at runtime, not at compile time.

Example

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

Output:

Dog barks

Explanation:

  • The reference is of type Animal

  • The object is of type Dog

  • Java decides at runtime which method to call

  • This is called runtime polymorphism


6. Rules for Method Overriding

For a method to be overridden, the following rules must be followed:

1. Same Method Name

The method name in the subclass must be exactly the same as in the superclass.

2. Same Parameters

The parameter list must be the same in:

  • Number

  • Type

  • Order

3. Same or Covariant Return Type

The return type must be:

  • The same, or

  • A subclass type (covariant return)

4. Access Level Cannot Be Reduced

  • The subclass method cannot have more restrictive access

  • Example:

    • publicprotected

    • protectedpublic

5. Cannot Override final Methods

A method declared as final cannot be overridden.

6. Cannot Override static Methods

Static methods belong to the class, not the object. They are hidden, not overridden.

7. Must Use Inheritance

Method overriding is possible only in inheritance.


7. Role of the @Override Annotation

The @Override annotation:

  • Is optional

  • Helps the compiler detect errors

  • Improves code readability

Example

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

If the method does not correctly override a parent method, the compiler will show an error.


8. Method Overriding vs Method Overloading

Many learners confuse overriding with overloading. They are different concepts.

Method Overriding Method Overloading
Happens in inheritance Happens in same class
Same method signature Different parameters
Runtime polymorphism Compile-time polymorphism
Parent–child relationship No inheritance needed

9. Access Modifiers and Method Overriding

Access modifiers play an important role in method overriding.

Example

class Parent {
protected void display() {
System.out.println("Parent display");
}
}
class Child extends Parent {
public void display() {
System.out.println(“Child display”);
}
}

Here:

  • Access level is increased from protected to public

  • This is allowed

But this is not allowed:

public void display() // parent
protected void display() // child ❌

10. Using super in Method Overriding

The super keyword is used to:

  • Call the parent class method

  • Access parent class members

Example

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

Output:

Animal makes a sound
Dog barks

This is useful when:

  • You want to extend, not replace, parent behavior


11. Real-Life Example of Method Overriding

Consider a banking system:

class Bank {
double getInterestRate() {
return 5.0;
}
}
class SBI extends Bank {
double getInterestRate() {
return 6.5;
}
}

class HDFC extends Bank {
double getInterestRate() {
return 7.0;
}
}

Each bank:

  • Overrides the same method

  • Provides its own interest rate

This is a practical use of method overriding.


12. Advantages of Method Overriding

  1. Enables runtime polymorphism

  2. Improves flexibility

  3. Supports code reusability

  4. Makes programs scalable

  5. Helps in real-world modeling


13. Common Mistakes by Learners

  1. Changing method parameters

  2. Reducing access level

  3. Forgetting inheritance

  4. Confusing overriding with overloading

  5. Not using @Override


14. Summary

Method overriding allows a subclass to provide its own implementation of a method inherited from a superclass.

Key Points to Remember

  • Requires inheritance

  • Same method signature

  • Happens at runtime

  • Supports polymorphism

  • Access level cannot be reduced

Method overriding is one of the most powerful features of OOP and is essential for writing flexible and reusable programs.