VOOZH about

URL: https://www.geeksforgeeks.org/java/overriding-in-java/

⇱ Overriding in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Overriding in Java

Last Updated : 13 May, 2026

Method overriding in Java allows a subclass to provide a specific implementation of a method that is already defined in its parent class. It is one of the key features of runtime polymorphism in object-oriented programming.

  • Access modifier cannot be more restrictive than the parent method
  • Achieved when child and parent classes have methods with the same signature
  • Static, final, and private methods cannot be overridden

Output
Dog is running.
Animal is eating.
Dog is barking.

Explanation: The Animal class defines base functionalities like move() and eat(). The Dog class inherits from Animal and overrides the move() method to provide a specific behavior Dog is running. Both classes can access their own methods. When creating a Dog object, calling move() executes the overridden method.

Rules for Method Overriding

  • Name, parameters, and return type must match the parent method.
  • Java picks which method to run at run time, based on the actual object type, not just the reference variable type.
  • Static methods cannot be overridden.
  • The @Override annotation catches mistakes like typos in method names.
👁 Method Overriding in Java

Special Cases in Overriding

1. Calling Parent Method Using super

The super keyword can invoke the parent class method from the overriding method.


Output
Parent's show()
Child's show()

2. Final Methods Cannot Be Overridden

If we don't want a method to be overridden, we declare it as final. Please see Using Final with Inheritance

Output:

👁 MethodOverriding

3. Static Methods

  • Static methods cannot be overridden; defining a static method in a subclass with the same signature as in the superclass hides the superclass method.
  • Instance methods can be overridden, but a subclass cannot override a superclass static method.
  • A static method in a subclass with the same signature as a superclass static method hides the original method.

Output
Parent static method
Child instance method

4. Private Methods

  • Private methods cannot be overridden because they are not visible to subclasses.
  • A subclass method with the same name is treated as a new, independent method, unrelated to the parent class.

Output
Child method

5. Covariant Return Types

  • In method overriding, the return type of the overriding method can be a subclass of the return type of the overridden method.
  • This feature is known as covariant return type and allows more specific return types in the subclass.

Output
Child object

Exception-Handling in Overriding

  • The overriding method cannot throw new or broader checked exceptions than the method in the superclass.
  • It can throw fewer or narrower checked exceptions.
  • It can throw any unchecked exceptions (like RuntimeException) regardless of the superclass method.

Output
Child method

Real-Life Example: Employee Management System

Let’s understand overriding with a real-world analogy.

Imagine an organization’s Employee Management System. All employees share some behaviors like raiseSalary() and promote(), but the logic differs for different roles like Manager or Engineer. We can create a single Employee array where individual employees are of different types (sales, tech, etc) and call their functions. This simplifies the overall code a lot.


Output
--- Raising Salaries ---
Manager salary raised with incentives.
Engineer salary raised with bonus.

--- Promotions ---
Manager promoted to Senior Manager.
Engineer promoted to Senior Engineer.

Explanation: Although both Manager and Engineer objects are referred to using the Employee type, Java calls the overridden methods of the actual objects at runtime, demonstrating dynamic method dispatch (runtime polymorphism).

Related Article: Method Overloading and Method Overriding

Comment
Article Tags:
Article Tags: