Method Overriding in Java

1
4

Method Overriding

If subclass/child class has the same method as declared in the super/parent class (signature of method in both class should be same), it is known as method overriding in java.

In other words, If subclass provides the specific implementation of the method that has been provided by one of its parent class, it is known as method overriding.

Rules of Method Overriding:

  1. method must have same name as in the parent class
  2. method must have same parameter as in the parent class.
  3. must be IS-A relationship (inheritance).

Let us take one example:

  1. class Car{
  2. void run(){System.out.println("Car is running");}
  3. }
  4. class Mercedes extends Car{
  5. void run(){System.out.println("Mercedes is running safely");}
  6.  
  7. public static void main(String args[]){
  8. Mercedes obj = new Mercedes ();
  9. obj.run();
  10. }
  11. }
Facebook Comments
(Visited 3 times, 1 visits today)

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here