Method Overriding Java

Method overriding is a feature in OOPS which allows the sub-class or child class to provide an implementation of a class that already have a super class or parent class.

Java overriding

If the method in a sub-class has a same name, same return type, same parameters in a super class then the method is said to override over a sub-class.

Here I'm trying to declare the Parent class first.

class Vehicle{
    void manufacturingyear() //local variable
    {
        System.out.println("Vehicle manufacturingyear "); //output 1
    }
}
Now here my vehicle which is a parent class will have child class in it.
This is what is called overriding.

  class Model extends Vehicle {
      @Override
      void manufacturingyear()
      {
          System.out.println("Model manufacturingyear"); //output 2
      }


Now I'm starting the main class declaration.
      
    
  }
  
  class Main {
      public static void main (String[] args) {
        Vehicle obj1 = new Vehicle(); //variable 1 declared
        obj1.manufacturingyear(); //first object
        
        Vehicle obj2 = new Model(); //variable 2 declared
        obj2.manufacturingyear(); //second object
      }
  }

        //output:

Scroll to top