简体   繁体   中英

Is there any harm in using super when not needed?

This message pertains strictly to Java. If a method is in a superclass there are two ways the method could be called:

foo();
super.foo();

Is there any harm in always doing the latter? As a coding style I prefer the latter because it's clear at a glance where the method call is coming from. Are there any circumstances where 'super' is going to be non-existent or not do what I think it would do?

I think the only harm you may have is when you want to use polymorphism so, if you call foo() and some subclass overrides foo, then the effect would be different than if you call super.foo() , basically, it depends on how you are designing the code and for what purpose.

Hope this makes it clear.

As a general rule, you should only use super.foo() inside your class foo() method. Doing otherwise, in general, goes against OOP thinking.

because it's clear at a glance where the method call is coming from

In OOP you should'n want to know where the method call "comes from". If your program (or your thinking) is depending on that, you are in potential trouble (and will probably be in actual trouble when someone decides to override the method). The method myobject.foo() must be seen from the outside as the method of the myobject's class; it should not matter if that method is implemented actually in the concrete class of its parent.

我会说做前者有更多的危害,因为它不是一个超类的方法。

Yeah this basically breaks the inheritance chain.

You don't allow the inheritance mechanism to choose what function to use even in classes derived from this one.

The point of super.foo() is to allow you to specify only when it is needed and you know no other behavior will be good.

If you do not want to explicitly avoid using an overriding method in the subclass then you should not use super. Always using super might cause trouble if later on someone wants to override the method in the subclass.

That's a preferable way if you intend to call the method on the super class, instead of calling foo() without super. . If anyone does overwrite foo() in the subclass the super call does call the same method as before, but omiting super will now call the overwritten method. It depends on what you intent with that method call.

It depends.

If foo() is declared as final , it will make no difference at all. If foo() is not declared as final , then a subclass could override the declaration of foo() in your superclass and completely change the expected behaviour.

If you make your own class final , you can prevent it from being sub-classed, and be certain the original intent is preserved.

I would be say that this might indicate that you should think over you design again.

If you are always calling the functionality by super.foo() then you block yourself from overriding the function later, and if you don't want to have the ability to override the function, then maybe you shouldn't use inheritance as a method for accessing this functionality.

One design principle that I have heard banded about is "favour composition over inheritance", the reason for this is that your code becomes more flexible with composition rather than inheritance. And if you don't gain the positive aspects of inheritance (being able to override the function) then maybe it's wiser to not use inheritance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM