Why is the use of super to call methods other than overridden not recommended?

The oracle textbook says: If your method overrides one of its superclass methods, you can call the method overridden with the super keyword.

He mentioned the use of super in an overridden method.

However, in reality, in the example program I wrote, I can use super words to access any method in the superclass.

The question here is: why do most people talk on the Internet about using super, do they always talk about calling the override method?

I mean, why is “using super to call another method in a superclass” not recommended?


By the way, one more question: we cannot use super in a static method. The compiler will not allow us to do this.

This is because the variable "super" belongs to the object instead of the class, just like the keyword "this" ?. The static method belongs to the class, but is there a super variable in the static method ?

+4
source share
5 answers

Why using the super.foo () method outside the foo () method is a bad idea

This suggests that the old method is still necessary (and does something different from the child version of the method), in which case you probably should not override it in the first place. The parent method still "makes sense" inside the child.

, , . , , , , .

super.foo() , ,

. ( ) - childs, , super.method(), method(), , . , , , , .

public , .

, , ( - ). , super , . , Foo

Foo.staticMethod();

+5

, , , , - .

... .

super , :

  • private , .

  • , , .

  • protected .

, , , , , , .

: , ?

, :

public class A {
    public void foo() {...}
}

public class B extends A {
    public void foo() {   // The overriding method.
        ...
        super.foo();      // invoking the overridden method.
        ...
    }
}

. , , , super. .

, A.foo() , super.foo() B... , . , , , B.

+2

super.f() this.f(). this.f(), super . , this.f().

: g(), super.f(): - .

: , , , ..

public class SpecialBoxLayout extends BoxLayout {

    @Override
    public void layoutHor(...) {
        ... // Something extra, special
        for
            super.layoutVert(...);
        ...
    }

    @Override
    public void layoutVert(...) {
        ...
        for
            super.layoutHor(...);
        ...
    }

, . . super.super.f().


(g super.f) , :

  • layout(Orientation.VERT, ...).
+1

You can use super.method()to call any method, but you can super()only use to call an overridden method.

0
source

Suppose you have classes A and B, where B extends A. Class A has methods: methodOne (), methodTwo (), and class B overrides the method One (). However, remember that class B also has a Two () method, since it is inherited, so it makes no sense to call it through super, because you have it directly in the descendant class.

EDIT. See what:

import java.util.*;
class A {
    public void methodOne() {
        System.out.println("A.1");
        methodTwo();
        //super.methodTwo(); 
            /*This above would lead to compilation error even though
            it would work as intended - always print A.2. 
            Use instead for instance: ((A)this).methodTwo();*/

    }

    public void methodTwo() {
        System.out.println("A.2");
    }
}

class B extends A{
    @Override
    public void methodTwo() {
        System.out.println("B.2");
    }
    public B() {
        methodOne();
    }
}

public class Test {
    public static void main(String[] args) {
        A a = new A();
        B b = new B();
        a.methodTwo();
        b.methodTwo();
    }
}
0
source

Source: https://habr.com/ru/post/1542095/


All Articles