What is the override function in OOPs?

1.1K    Asked by AndreaBailey in Salesforce , Asked on Aug 1, 2020
Answered by Andrea Bailey

Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. When a method in a subclass has the same name, same parameters or signature and the same return type(or sub-type) as a method in its super-class, then the method in the sub-class is said to override the method in the super-class. Method overriding is one way to achieve polymorphism.

Example :-

/ Base Class

class testclass {

 void show()

 {

  System.out.println("Testclass show()");

 }

}

// Inherited class

class Child extends testclass {

 // This method overrides show() of Parent

 @Override

 void show()

 {

  System.out.println("Child's show()");

 }

}

// Driver class

class Main {

 public static void main(String[] args)

 {

  // If a Parent type reference refers

  // to a Parent object, then Parent's

  // show is called

  testclass obj1 = new testclass();

  obj1.show();

  // If a Parent type reference refers

  // to a Child object Child's show()

  // is called. This is called RUN TIME

  // POLYMORPHISM.

  testclass obj2 = new Child();

  obj2.show();

 }

}



Your Answer

Interviews

Parent Categories