Method Overriding

Method Overriding

Redefining of the parent class method in the child class is known as method overriding. Method name remains the same but the implementation of the same changes. It helps to implement runtime polymorphism.

This enable to provide the same method implementation in the subclass when the super class uses the same signature before. Thus this method overriding adds as an distinct feature of Java programming

Based on the invocation the methods can be accessed, if the object of the super class is invoked , the method associated with that class will be called, if the invocation if of the sub class or the child class, that method is called.

Following are certain rules to be followed while performing the method overriding

  • Concern towards the Access Modifiers
  • The methods which are declared final cannot be overridden
  • The methods which are static cannot be overridden
  • It is mandatory that the return type should be same with the overridden method
  • The super keyword can be used to invoke the base class method if needed
  • We cannot override a constructor


Com.knowledge2life public class Main{ void run(){System.out.println("Knowledge2life");} } class ride extends Main{ void run(){System.out.println("Knowledge2life learning website");} public static void main(String args[]){ ride obj = new ride();//creating object;//calling method } }