Why can a subclass access the private method of the parent class?

Source: Internet
Author: User
{Code...} output: privatef () deribar why?
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();    }}

Output:
Private f ()
Deri bar

Why?

Reply content:
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();    }}

Output:
Private f ()
Deri bar

Why?

  1. First, it is not called directly.privateOff()Method, just likegetterMethod to obtainprivateThe instance variables are the same.

  2. Here is an inheritance question:DerivedClassF methodNot its parent classPrivateOverride f methodBecause the override method requires that the access permission modifier of the method after rewriting cannot be stricter than that of the parent class.helloMethodthis.f(), Compiler priorityRecentf(That is,f).private f().
    Of course, in actual development, we should try to avoid such confusion and do basic interview questions.

You can access a private method through a public method.

Public void hello () is a public method
It is a private method that allows calls to this class.

According to the above example, it cannot be said that the subclass can access the private method of the parent class. BecausehelloItself is the method of the parent class.
If the subclass is like this:

class Derived extends PrivateOverride {    public void hello() {        System.out.println("public f()");    }    public void bar(){        this.f();        //...    }}

If it runs properly, the subclass can access the private method of the parent class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.