Loading

Method Overriding

  • Method overriding is used for runtime polymorphism when a subclass or child class has a method that is identical to one in the parent class.

Key Points:

  • 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.