Java extends the static method, cannot compile, why?

class Base {
    public static void f(){}
}
class Derived extends Base{
    private static void f(){}
}

the Derived.f () modifier must be public, if it is closed, the compiler says "an attempt to assign weaker access rights was public."
I do not understand! Why does the compiler not allow us to do this?

+4
source share
2 answers

, , . , . , .

+1

, ( private) , ( public), .

(LSP), - , : , , .

:

public class Base {
    private static void f() {
    }
}

class Derived extends Base {
    public static void f() {    // as well as protected, default, private
    }
}
+1

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


All Articles