Using super.method () if you do not override the method?

Is it common practice to always use super to call methods outside the superclass, even if I DO NOT override the method?

Suppose

public class Parent{ public void method() { } } 

So

 public class Child extends Parent { public void someMethod() { super.method(); } } 

or

 public class Child extends Parent { public void someMethod() { method(); } } 

Thanks for your input.

+4
source share
6 answers

The answer to the main question is easy to answer:

Is it common practice to always use super to call methods outside the superclass, even if I DO NOT override the method?

No, this is not a common practice. On the contrary, it is dangerously confusing. You usually call methods because the main point of inheritance is to allow classes to override methods to modify / extend their behavior. Usually you don’t care at what level the method is actually implemented.

The obvious exception is that you yourself override the method and want / need the functionality of the parents.

Now to the dangerously confusing part of the explicit super call:

 public class CallSuper { static class Parent { public void foo() { System.out.println("Parent.foo"); } /** * Calls foo to do the main work */ public void bar() { System.out.print foo(); } } static class Child extends Parent { public void foo() { System.out.println("Child.foo"); } public void bar() { super.foo(); } } static class GrandChild extends Child { public void foo() { System.out.println("GrandChild.foo"); } } public static void main(String[] args) { Parent p = new Parent(); Parent c = new Child(); Parent g = new GrandChild(); System.out.print("Parent.foo() = "); p.foo(); System.out.print("Child.foo() = "); c.foo(); System.out.print("GrandChild.foo() = "); g.foo(); System.out.print("Parent.bar() = "); p.bar(); System.out.print("Child.bar() = "); c.bar(); System.out.print("GrandChild.bar() = "); g.bar(); } } 

If you run this example, it will output (added line numbers for clarity):

 1 Parent.foo() = Parent.foo 2 Child.foo() = Child.foo 3 GrandChild.foo() = GrandChild.foo 4 Parent.bar() = Parent.foo 5 Child.bar() = Parent.foo 6 GrandChild.bar() = Parent.foo 

The first four lines are not surprising, we get what foo implements at each level, respectively, Parents bar, calling it foo. It starts to become odd on line 5. The child bypasses its own override foo by calling super.bar (), so when its foo () is specialized, bar () is not. On line 6, you see that GrandChild inherits this oddity from Child, so the somewhat innocent look super.foo () in the child bar has now broken GrandChild's expectation that its redefinition of foo () is also effective for all bar ().

Now, if you imagine that foo () and bar () are actually doing something useful and that they have a meaningful relationship with each other, for example. you believe (either through parenting documentation or just common sense) that overriding foo () will also change the behavior of bar (). The “super” child breaks this.

Generally, if you see a call to "super.x ()" somewhere outside of the actual override of "x ()", this may happen by accident.

+2
source

The super call tells the JVM to explicitly look at the version of the parent class method. Continuing your example, if you just call

 method() 

The JVM will first search in the calling class for the method (in this case, Child ) before looking at the parent class. On the other hand, if you call

 super.method() 

then the JVM will explicitly consider the parent class to implement, even if Child has a method called method() .

Combining these two ideas, you should probably always use super when you intend to call a method of the parent class. If your class does not override the method, you can call without super . But even this becomes problematic if someone had to reorganize your class later and override the method.

+3
source

Is it common practice to always use super to call methods from a superclass? Or is it redundant code?

This is not redundant. It has a special purpose to call the method of the parent class (although you are ovveriden). When you do not want, do not call. If the current class is not a child of this class, super will not work for you.

Also, if you call an overridden super method, does it use a super method or the lowest method? (in the inheritance tree)

Overridden method. Since you redefined it in your class.

+2
source

The method call is super or completely dependent on what you are trying to do. If you want to do what the parent class did, plus some additional action, then calling the super method is good. If you are doing something completely different in the implementation of the child, then do not call the super method. When you call a super method in a class hierarchy, it calls the one closest to your child class in the hierarchy.

In some cases, you may have an API where calling a super method is part of the general contract. The clone method is such a case.

0
source

If the child has the same version method with the parent, the JVM will invoke the Child version instead of the parent version, if not super. The super keyword is useful if you want your child to always call the parent version method.

0
source

If you do not override, tell us about performance.

If you call an inherited method in your class, say methodInSuperclass() , the JVM will look for that method first in your class and then up the class hierarchy until it finds it.

Using super.methodInSuperclass() tells the JVM to go directly to the superclass to find this method.

Thus, the use of the super keyword improves the performance of several plants. This is ugly code, but it is not redundant, as it will bring some performance improvement.

Think about it if the time is critical.

0
source

All Articles