Is Super.someMethod() a good practice to add clarity to the code by explicitly stating that someMethod is not a method included in the current class but in the parent class?

For instance, would this make sense...

private void someMethod() {
    super.someOtherMethod(); // this is in the parent class
    otherMethod(); // this can be found here

Instead of this?

private void someMethod() {

If this is not a good idea/practice, is there any convention on this aspect that helps clarify where the method is?

Note: please take into consideration that I am not asking about disambiguating calls to overridden methods! In that particular case, to the best of my knowledge, using Super is a must in order to invoke the correct method.

  • 1
    Your 2nd snippet is not an option. This will simply result in stack overflow due to infinite recursion. The former snippet is the only choice here. If your question is around something else, maybe provide some more representative example code. – Erik Eidt Jun 15 '16 at 15:57
  • @ErikEidt Yes. That's an error on the example. I just fixed it... – carlossierra Jun 15 '16 at 16:00

I would consider this not just bad practise but thwarting the principles of OOP because a super call is a different form of call: it is a direct call that bypasses the virtual dispatch mechanism (this is how infinite recursion is avoided when you are trying to call up the class hierarchy from an override).

When you switch methods, e.g. from someMethod to someOtherMethod, typically in OOP, you want a new full virtual dispatch for the someOtherMethod invocation, which is to say that you want the first handler to be the true class of the object, not some base class.

Consider that the true class of the object (for this) is really some subclass, XYZ, that the current lines of code in your example don't know about (and shouldn't have to). That subclass may have chosen to override someOtherMethod, and when it is invoked (from an client outside the class hierarchy, and even from within the implementation of some other method within the class hierarchy), that's where the call should go.

If we didn't have full virtual dispatch for virtual methods invoked from within base class method implementations, much of the power of the OOP virtual method and override mechanism would be lost.

In short (unless you really know what you're doing and you're trying to do something unusual), you should only use a super call to call up the class hierarchy on the same method and from within an override of that method. (Some languages won't even let you invoke super on another method or outside an override.)

| improve this answer | |

Explicitly calling super.someMethod() may confuse a reader who is expecting to see that in the context of disambiguating a call.

From the Oracle docs on super:

[...] you can invoke the overridden method through the use of the keyword super.

It doesn't look like it should typically be used for clarity of where the method lives.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.