这是面试问题。

子类继承private吗 字段?

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

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

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

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


当前回答

It would seem that a subclass does inherit the private fields in that these very fields are utilized in the inner workings of the subclass (philosophically speaking). A subclass, in its constructor, calls the superclass constructor. The superclass private fields are obviously inherited by the subclass calling the superclass constructor if the superclass constructor has initialized these fields in its constructor. That's just an example. But of course without accessor methods the subclass cannot access the superclass private fields (it's like not being able to pop the back panel of an iPhone to take the battery out to reset the phone... but the battery is still there).

PS 我遇到的关于继承的许多定义之一是: “继承——一种编程技术,允许派生类扩展基类的功能,继承其所有的STATE(重点是我的)和行为。”

私有字段即使不能被子类访问,也是父类的继承状态。

其他回答

我可以试着帮你。 当一个子类(例如命名为B)扩展一个超类(例如命名为a)时,它自动从它的超类继承字段(例如属性和/或方法)。 现在,B在它的内存布局中为类A中的每个字段提供了空间,包括私有字段。事实上,Java不允许子类B使用私有字段,因为它们是私有的。

好吧,这是一个非常有趣的问题,我研究了很多,得出的结论是,父类的私有成员确实在子类的对象中可用(但不可访问)。为了证明这一点,这里是一个带有父类和子类的示例代码,我正在将子类对象写入txt文件,并在文件中读取名为“bhavesh”的私有成员,因此证明它确实在子类中可用,但由于访问修饰符而不可访问。

import java.io.Serializable;
public class ParentClass implements Serializable {
public ParentClass() {

}

public int a=32131,b,c;

private int bhavesh=5555,rr,weq,refw;
}

import java.io.*;
import java.io.Serializable;
public class ChildClass extends ParentClass{
public ChildClass() {
super();
}

public static void main(String[] args) {
ChildClass childObj = new ChildClass();
ObjectOutputStream oos;
try {
        oos = new ObjectOutputStream(new FileOutputStream("C:\\MyData1.txt"));
        oos.writeObject(childObj); //Writing child class object and not parent class object
        System.out.println("Writing complete !");
    } catch (IOException e) {
    }


}
}

打开MyData1.txt,搜索名为“bhavesh”的私有成员。请告诉我你们的想法。

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成员,那么事情就搞砸了。

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

这里的问题/答案中的大多数困惑都围绕着继承的定义。

显然,正如@DigitalRoss解释的那样,子类的OBJECT必须包含其父类的私有字段。正如他所说,无法访问私人会员并不意味着它不存在。

然而。这与类继承的概念不同。在java世界中,存在语义问题时,仲裁人是java语言规范(目前是第3版)。

正如JLS所述(https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.2):

已声明的类的成员 Private不被继承 该类的子类。只有成员 声明为受保护的类的 或public由子类继承 类以外的包中声明 声明类的语句。

这就解决了采访者提出的问题:“子类继承私有字段吗?”(我强调了一下)

答案是否定的。他们没有。子类的对象包含父类的私有字段。子类本身没有父类的私有字段的概念。

这是学究性质的语义学吗?是的。这是个有用的面试问题吗?可能不会。但是JLS为Java世界建立了定义,并且这样做(在本例中)是明确的。

编辑(删除了Bjarne Stroustrup的平行引用,由于java和c++之间的差异,这可能只会增加混淆。我将让我的答案取决于JLS:)