简体   繁体   中英

Why private members are actually inherited by subclass

I know the Doc/Oracle indicates that subclass don't inherit private members from superclass.

But when debugging a program, we can definitely see "private members are inherited" if we trace the variables using ctrl F7 and Ctrl F4. That is quite strange.

Even if I override the default constructor with empty {} , it actually still inherit private members, though private members are not accessible.

Quite confusing between terminology and real work.

I created a sample, with three class:

public class Inheritance {        

public static void main(String[] args) {

    teacher t1 = new teacher();
    t1.setGrd(80);
    System.out.println(t1.getGrd());
}
}


public class person {
    private int age=90;
    public void person(){

    }
}

public class teacher extends person {
    private int grade;

    public void setGrd(int age){
        grade=age;
    }
    public int getGrd(){
        return grade;
    }
} 

When tracing it:

在此处输入图片说明

I think the documentation means that the sub-class cannot see/access private members of the super-class. That doesn't mean they don't still exist in the super-class. The debugger is showing the full state of the instance, which includes the private state of the super-class.

But when debugging a program, we can definitely see "private members are inherited" if we trace the variables using ctrl F7 and Ctrl F4.

Not inherited - the IDE shows all 1 fields of the type hierarchy to help with debugging. It does so via reflection by something like traversing all superclasses ( getSuperclass ) and collecting all the fields ( getDeclaredFields ). There are no language semantics in play here, it's a "hack".

I don't know what IDE you are using, but that "Inherited" label is not a precise choice of words, although it brings the point across well. You can file a ticket with the maintainers asking to use a more precise word.

1 IDEs can give you options to enable/disable what it shows, like constants ( static final ) or static fields, which are also not inherited.

Not inheriting private members would definitely violate Liskov subistution principle , so I think you got it the wrong way. Suppose a simple example:

public Foo {
  private int x = 50;
  public void yell() { System.out.println(x); }
}

public Bar extends Foo {

}

Bar bar = new Bar();
bar.yell();

What it is supposed to happen here if a private field is not inherited? You see that something is wrong in your deductions. private members are all inherited but can't be accessed.

This may sound useless but it is not since you could have a lot of functionality implemented in super type methods which rely on internal state which are not needed by inerithing classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM