Tuesday, 5 May 2020

Method Overriding in Java

If subclass has the same method as declared in the super class, it is known as method overriding in Java. It is required in the case, when we want to use the implementation of the child class instead of parent class.

Usage of Java Method Overriding

  • Method overriding is used to provide the specific implementation of a method in the child class which is already provided by its parent class.
  • Runtime polymorphism is used through method overriding.

Rules for Java Method Overriding

  • The method in the subclass must have the same name as in the super class.
  • The method must have the same parameter as in the parent class.
  • There must be an IS-A relationship (inheritance concept must be used).

Example of method overriding


//Java Program understand the use of Method Overriding 

// parent class. 

class Vehicle{ 

  //defining a method 

  void run(){System.out.println("Vehicle is running");} 


// child class 

class Bike extends Vehicle{ 

  //defining the same method as in the parent class 

  void run(){System.out.println("Bike is running safely");} 


  public static void main(String args[]){ 

  Bike obj = new Bike2();//creating object 

  obj.run();//calling method 




Bike is running safely


No comments:

Post a comment

All about journals and research paper | What is impact factor? | how the impact factor is calculated? | who calculate the impact factor? Scopous journals

Figure 1: One of my Research Papers When the Scholars are in their Master or PhD or in any research field. They are supposed to writ...