这是面试问题。

子类继承private吗 字段?

我的回答是“不”,因为我们不能使用“正常的面向对象的方式”访问它们。但是采访者认为它们是继承的,因为我们可以间接或使用反射访问这些字段,并且它们仍然存在于对象中。

回来后,我在javadoc中找到了以下引用:

超类中的私人成员 一个 子类不继承私有 父类的成员。

你知道面试官观点的论据吗?


当前回答

这取决于你对“继承”的定义。子类在内存中仍然有字段吗?肯定。它能直接访问它们吗?不。这只是定义的微妙之处;关键是要理解到底发生了什么。

其他回答

我相信,答案完全取决于所提出的问题。我的意思是,如果问题是

我们可以直接访问超类的私有字段吗 他们的子类?

那么答案是否定的,如果我们查看访问说明符的细节,就会提到,私有成员只能在类本身内访问。

但是,如果问题是

我们可以访问超类的私有字段吗 他们的子类?

这意味着,访问私有成员的操作并不重要。在这种情况下,我们可以在超类中创建public方法,你可以访问private成员。因此,在本例中,您创建了一个接口/桥接器来访问私有成员。

其他oop语言,如c++,有友元函数的概念,通过它我们可以访问其他类的私有成员。

这取决于你对“继承”的定义。子类在内存中仍然有字段吗?肯定。它能直接访问它们吗?不。这只是定义的微妙之处;关键是要理解到底发生了什么。

正如其他人所指出的那样:

https://docs.oracle.com/javase/specs/jls/se8/html/jls - 8. - html # jls - 8.2:

声明为private的类的成员不会被继承 该类的子类。只声明类的成员 Protected或public由包中声明的子类继承 除了声明类的那个。

答案无疑是否定的,没有任何“如果”和“但是”。这围绕着继承的定义。根据定义,继承是针对类的,不是针对对象的。对象是使用类定义创建的。继承只是添加到类定义中的另一个块。那么,类是否继承了父类的私有成员?没有

我将用代码演示这个概念。子类实际上继承了父类的私有变量。唯一的问题是他们不能访问 子对象,除非您为私有变量提供公共getter和setter 在超级班。

考虑包转储中的两个类。子扩展父。

如果我没记错的话,内存中的子对象由两个区域组成。一个是父部分,另一个是子部分。子程序可以访问私有文件 节只能通过父类中的公共方法在其父类的代码中调用。

这样想。波拉特的父亲博尔托克有一个装有10万美元的保险箱。他不想共享他的“私有”变量保险箱。所以,他没有提供保险箱的钥匙。波拉特继承了保险箱。但是,如果他连门都打不开,那又有什么用呢?要是他的 爸爸提供了钥匙。

父母,

package Dump;

public class Parent {

    private String reallyHidden;
    private String notReallyHidden;

    public String getNotReallyHidden() {
        return notReallyHidden;
    }

    public void setNotReallyHidden(String notReallyHidden) {
        this.notReallyHidden = notReallyHidden;
    }

}//Parent

孩子,

package Dump;

public class Child extends Parent {

    private String childOnly;

    public String getChildOnly() {
        return childOnly;
    }

    public void setChildOnly(String childOnly) {
        this.childOnly = childOnly;
    }

    public static void main(String [] args){

        System.out.println("Testing...");
        Child c1 = new Child();
        c1.setChildOnly("childOnly");
        c1.setNotReallyHidden("notReallyHidden");

        //Attempting to access parent's reallyHidden
            c1.reallyHidden;//Does not even compile

    }//main

}//Child

Well, my answer to interviewer's question is - Private members are not inherited in sub-classes but they are accessible to subclass or subclass's object only via public getter or setter methods or any such appropriate methods of original class. The normal practice is to keep the members private and access them using getter and setter methods which are public. So whats the point in only inheriting getter and setter methods when the private member they deal with are not available to the object? Here 'inherited' simply means it is available directly in the sub-class to play around by newly introduced methods in sub-class.

将下面的文件保存为ParentClass.java并自己尝试它->

public class ParentClass {
  private int x;

  public int getX() {
    return x;
  }

  public void setX(int x) {
    this.x = x;
  }
}

class SubClass extends ParentClass {
  private int y;

  public int getY() {
    return y;
  }

  public void setY(int y) {
    this.y = y;
  }

  public void setXofParent(int x) {
    setX(x); 
  }
}

class Main {
  public static void main(String[] args) {
    SubClass s = new SubClass();
    s.setX(10);
    s.setY(12);
    System.out.println("X is :"+s.getX());
    System.out.println("Y is :"+s.getY());
    s.setXofParent(13);
    System.out.println("Now X is :"+s.getX());
  }
}

Output:
X is :10
Y is :12
Now X is :13

如果我们尝试在子类的方法中使用ParentClass的私有变量x,那么它不能被任何修改直接访问(意味着不能继承)。但是x可以通过原始类的setX()方法在子类中修改,就像在setXofParent()方法中做的那样,或者可以使用ChildClass对象使用setX()方法或setXofParent()方法修改,最终调用setX()。这里setX()和getX()是一种通往ParentClass的私有成员x的门。

另一个简单的例子是Clock超类将hours和min作为私有成员,将适当的getter和setter方法作为公共方法。然后是数字时钟作为时钟的子类。在这里,如果DigitalClock的对象不包含hours和mins成员,那么事情就搞砸了。