Home  >  Article  >  Backend Development  >  Why can subclasses access private methods of parent class?

Why can subclasses access private methods of parent class?

WBOY
WBOYOriginal
2016-08-20 09:03:591554browse

<code>class PrivateOverride {

    private void f() {
        System.out.println("private f()");
    }
    public void hello(){
        this.f();
        this.bar();
    }

    protected void bar(){
        System.out.print("pri bar");
    }
}

class Derived extends PrivateOverride {

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

    public void bar(){
        System.out.print("deri bar");
    }
}
class Test4{
    public static void main(String[] args) {


        Derived po = new Derived();


        po.hello();
    }
}</code>

Output:
private f()
deri bar

Why

Reply content:

<code>class PrivateOverride {

    private void f() {
        System.out.println("private f()");
    }
    public void hello(){
        this.f();
        this.bar();
    }

    protected void bar(){
        System.out.print("pri bar");
    }
}

class Derived extends PrivateOverride {

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

    public void bar(){
        System.out.print("deri bar");
    }
}
class Test4{
    public static void main(String[] args) {


        Derived po = new Derived();


        po.hello();
    }
}</code>

Output:
private f()
deri bar

Why

  1. First of all, this is not a direct call to the f() method of private, just like the getter method can get the private instance variable. This needs no explanation.

  2. There is an inheritance problem here: the f method in the Derived class is not an override of the PrivateOverride f method of its parent class, because the overridden method requires that the access permission modifier of the overridden method cannot be higher than the parent class’s PrivateOverride f method The class is more strict. When executing this.f() in the hello method, the compiler gives priority to finding the nearest f (that is, the f of the parent class), so
    private f()

    is typed.
  3. Of course, in actual development, we should try to avoid this kind of confusion, and it’s okay to do basic interview questions.

You are accessing private methods through public methods, this is okay


public void hello() is a public method

It is a private method that allows calling this class

According to your above example, it does not mean that the subclass can access the private method of the parent class. Because
hello

itself is a method of the parent class.

If the subclass is like this:

<code>class Derived extends PrivateOverride {

    public void hello() {
        System.out.println("public f()");
    }

    public void bar(){
        this.f();
        //...
    }
}</code>
🎜If it can run normally, it means that the subclass can access the private method of the parent class. 🎜
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn