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:
-
Supports runtime polymorphism
-
Allows customized behavior in child classes
-
Makes programs more flexible and extensible
-
Helps in code reusability
-
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
Here:
-
Doginherits thesound()method fromAnimal
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
In this example:
-
Dogoverrides thesound()method -
When
sound()is called on aDogobject, the child class version runs
5. How Method Overriding Works
Method overriding is resolved at runtime, not at compile time.
Example
Output:
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:
-
public→protected❌ -
protected→public✅
-
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
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
Here:
-
Access level is increased from
protectedtopublic -
This is allowed
But this is not allowed:
10. Using super in Method Overriding
The super keyword is used to:
-
Call the parent class method
-
Access parent class members
Example
Output:
This is useful when:
-
You want to extend, not replace, parent behavior
11. Real-Life Example of Method Overriding
Consider a banking system:
Each bank:
-
Overrides the same method
-
Provides its own interest rate
This is a practical use of method overriding.
12. Advantages of Method Overriding
-
Enables runtime polymorphism
-
Improves flexibility
-
Supports code reusability
-
Makes programs scalable
-
Helps in real-world modeling
13. Common Mistakes by Learners
-
Changing method parameters
-
Reducing access level
-
Forgetting inheritance
-
Confusing overriding with overloading
-
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.