Start Coding Now

Method Overriding in Java - Runtime Polymorphism

Understand method overriding. How a subclass allows to provide specific implementation of a method executing in superclass.

Method Overriding

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

Rules:

  • Method must have same name as in parent class.
  • Must have same parameters.
  • Must be IS-A relationship (inheritance).
  • class Vehicle {
      void run() { System.out.println("Vehicle is running"); }
    }

    class Bike extends Vehicle { @Override void run() { System.out.println("Bike is running safely"); } public static void main(String args[]) { Bike obj = new Bike(); obj.run(); // Calls Bike's run } }

    Frequently Asked Questions