Confused with Java Access Level Override

Possible duplicate:
Why can't you reduce the visibility of a method in a java subclass?

Why can I override a method privatein a superclass with publicwhen in a subclass, but I can not override a method publicin a superclass in a method privatein a subclass?

Why?

Thanks in advance.

+3
source share
5 answers

. , (), B , A. "" foo, "", B , protected.

, private ( !), . public , private , , , . private public , !

: private .

:

public static class Base {
    public void callBoth() {
        foo();
        bar();
    }

    private void foo() {
        System.out.println("Base.foo");
    }

    protected void bar() {
        System.out.println("Base.bar");
    }
}

public static class Sub extends Base {
    public void foo() {
        System.out.println("Sub.foo");
    }

    public void bar() {
        System.out.println("Sub.bar");
    }
}

new Sub().callBoth() :

Base.foo
Sub.bar
+11

, . Kitten Animal Animal feed(), feed(), Kitten Animal. .

+1

, ( ).

, ( / ).

+1

, .

private → [default] → protected → public

+1

, , api, -. - - .

private ( ), , "". , , , , .

+1

All Articles