Loading

Method overriding is Java feature in which it's allows a child class to redefine a method that is already in parent class. it is used for runtime polymorphism means the correct method is chosen at runtime instead of compile time.


Why We Need Method Overriding ?

A parent class may define a general method, but a child class can modify it to be more specific.

Example: A general vehicle moves, but a car moves in a different way. 

Rules of Method Overriding 

1. Same Method Name: The method in the child class must have the same name as in the parent class.

2. Same Parameters: The number and type of parameters must be exactly the same in both parent and child class methods.

3. Inheritance is Required: Method overriding only works with inheritance (i.e. when one class extends another).

4. Return type can be the same: The child class can return the same type or a more specific type (called covariant return type).

5. Access Modifier Rules:

  • If a method is public in the parent class, it must be public or protected in the child class.
  • A protected method in the parent can be protected or public in the child.
  • You cannot make a parent method more restrictive (e.g. changing public to private).

6. Final, Static and Private Methods Cannot Be Overridden:

  • A final method cannot be change in the child class.
  • Static methods belong to the class, not an object, so they are not overridden but can be redeclared.
  • Private methods are not inherited, so they cannot be overridden.

Key Point

  • It is only possible by inheritance.
  • The method must have the same name and the same parameters as the parent class.


Example:

package quipoHouse;

public class Overriding {
	void message(String msg) {
		System.out.println("Hello");
	}
}

class Main extends Overriding {
	void message() {
		System.out.println("QUIPO HOUSE");
	}

	public static void main(String[] args) {
		Main obj = new Main();
		obj.message(); // calling method
	}
}


Output:

QUIPO HOUSE


Here the output is from class main because we create an object of the main class.


Why Use Overriding ?

  • It prevents mistakes (e.g., if the method signature doesn't match it throws an error).
  • It makes easy to read and understand the code.
Using Super to Call Parent Class Method

Sometimes, we might want to override a method but still call the parent inside it. This is where the super keyword help.

Example:
class Vehicle { void run() { System.out.println("Vehicle is running"); } } class Car extends Vehicle { @Override void run() { super.run(); // Calls parent class method System.out.println("Car is running"); } } public class Main { public static void main(String[] args) { Car myCar = new Car(); myCar.run(); } }

Output:
Vehicle is running Car is running

Here, super.run(); first calls the parent class method, and then the child class method executes.

Two Minute Drill:

  • Method Overriding is a fundamental concept in Java that allows a child class to customize or modify a method from its parent class.
  • It helps in achieving runtime polymorphism, allowing Java programs to be more flexible and reusable.
  • Using override is a best practice to avoid errors while overriding methods.
  • The super keyword helps in calling the parent class method when needed.