3

Recently I was asked this question "why should one override a method? "

I replied, if I have a class with 10 methods and I want to use all of its functionality except one method, then I will override that method to have my own functionality.

Then the interviewer replied in that case why cant we write a new method with a different name and use that method instead.

Yes this is also right. Now I am confused. What is the real objective in overriding a method?

Can anyone please tell me? Thank you all in advance.

Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58
Krishna Chaitanya
  • 2,533
  • 4
  • 40
  • 74

7 Answers7

4

If you will give another name to the method in derived class, you cant invoke it with same interface. You can always invoke it through base class pointer.

i.e.

Base p = new Derived();
p.overrideMethod();

If Derived class is derived from Base then it will automatically call the derived version and not of Base. In case of different name, it is not possible. It is called code against interfaces and not implementations.

Apurv
  • 17,116
  • 8
  • 51
  • 67
1

I replied, if I have a class with 10 methods and I want to use all of its functionality except one method, then I will override that method to have my own functionality.

=> Very often a way to break the Liskov Substitution principle ... => very bad OO design

You have many examples on the web of this "break" but a you can find a good explanation here.

Mik378
  • 21,881
  • 15
  • 82
  • 180
1

.

why cant we write a new method with a different name and use that method instead

It is because we want to use polymorphism. You could tell the interviewer this example: There is a module that calls specific methods on objects you give it; now imagine you can't change that module (e.g. no source). You can't tell it to use a different method but you can give it an object of a subclass which has overridden that method. To the module it will appear that nothing changed.

In practice it is also often the case that you could change that module but dont want to.

Bernd Elkemann
  • 23,242
  • 4
  • 37
  • 66
0

The benefit of overriding is: ability to define a behavior that's specific to the subclass type which means a subclass can implement a parent class method based on its requirement.

Sanjay Bhimani
  • 1,593
  • 1
  • 15
  • 29
0

One uses interfaces to allow for multiple implementations and one uses overriding to simplify the implementation of an interface (e.g. when implementing a WindowListener, one typically extends and overrides a method of WindowAdapter so that one does not need to provide definitions for the cases where the default behavior is sufficient). Adding a new method rather than overriding would not work in this case, because the caller understands the interface and invokes its methods; the whole point of overriding here is to change the behavior for the calls to the interface. If you simply added a new function, then the caller would have to know about it, which defeats the entire isolation between the consumer of a piece of functionality and the provider of that functionality which is what interfaces are intended to provide.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
0

Overriding is a feature that is available while using Inheritance.

It is used when a class that extends from another class wants to use most of the feature of the parent class and wants to implement specific functionality in certain cases.

In such cases we can create methods with the same name and signature as in the parent class. This way the new method masks the parent method and would get invoked by default.

rachana
  • 3,344
  • 7
  • 30
  • 49
0

The main objective of overriding is code reuseablity which can be advatageous in big projects,it also provide flexiblity means you can pass different sets of input from any class and get the output

codegasmer
  • 1,462
  • 1
  • 15
  • 47