METHOD OVERRIDING IN JAVA

Method Overriding in Java






Isubclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java.

Usage of Java Method Overriding

  • Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.
  • Method overriding is used for runtime polymorphism

Rules for Java Method Overriding

  1. The method must have the same name as in the parent class
  2. The method must have the same parameter as in the parent class.
  3. There must be an IS-A relationship (inheritance).
Real life example


Eg





class Vehicle{  
  //defining a method  
  void run()
{
System.out.println("Vehicle is running");
}  
      }  

//Creating a child class  

class Bike extends Vehicle{  

  void run()
{
System.out.println("Bike is running fast");
}  
  
  public static void main(String args[]){  
  Bike2 obj = new Bike2();//creating object  
  obj.run();//calling method  
  }  
}  








Comments

Popular posts from this blog

OBJECT ORIENTED CONCEPTS

Arrays programms