这是面试问题。
子类继承private吗 字段?
我的回答是“不”,因为我们不能使用“正常的面向对象的方式”访问它们。但是采访者认为它们是继承的,因为我们可以间接或使用反射访问这些字段,并且它们仍然存在于对象中。
回来后,我在javadoc中找到了以下引用:
超类中的私人成员 一个 子类不继承私有 父类的成员。
你知道面试官观点的论据吗?
这是面试问题。
子类继承private吗 字段?
我的回答是“不”,因为我们不能使用“正常的面向对象的方式”访问它们。但是采访者认为它们是继承的,因为我们可以间接或使用反射访问这些字段,并且它们仍然存在于对象中。
回来后,我在javadoc中找到了以下引用:
超类中的私人成员 一个 子类不继承私有 父类的成员。
你知道面试官观点的论据吗?
当前回答
我必须回答,Java中的私有字段是继承的。请允许我演示一下:
public class Foo {
private int x; // This is the private field.
public Foo() {
x = 0; // Sets int x to 0.
}
//The following methods are declared "final" so that they can't be overridden.
public final void update() { x++; } // Increments x by 1.
public final int getX() { return x; } // Returns the x value.
}
public class Bar extends Foo {
public Bar() {
super(); // Because this extends a class with a constructor, it is required to run before anything else.
update(); //Runs the inherited update() method twice
update();
System.out.println(getX()); // Prints the inherited "x" int.
}
}
如果你在一个程序中运行Bar Bar = new Bar();,那么你总是会在输出框中看到数字“2”。由于整数“x”被update()和getX()方法封装,因此可以证明该整数是继承的。
令人困惑的是,因为不能直接访问整数“x”,所以人们认为它不是继承的。然而,类中的每一个非静态的东西,无论是字段还是方法,都是继承的。
其他回答
Yes
重要的是要认识到,虽然有两个类,但只有一个对象。
当然,它继承了私有字段。它们大概是正确的对象功能所必需的,虽然父类的对象不是派生类的对象,但派生类的实例基本上肯定是父类的实例。如果没有所有的领域,就不可能是这样。
不,你不能直接访问它们。是的,它们是遗传的。他们必须如此。
这是个好问题!
更新:
呃,“不”
我想我们都学到了一些东西。由于JLS提出了确切的“不继承”措辞,所以回答“不继承”是正确的。由于子类不能访问或修改私有字段,因此,换句话说,它们不会被继承。但实际上只有一个对象,它确实包含私有字段,因此如果有人以错误的方式使用JLS和教程措辞,将很难理解OOP、Java对象以及实际发生的事情。
更新更新:
这里的争议涉及到一个根本的模糊:讨论的究竟是什么?的对象?或者我们在某种意义上谈论的是类本身?当描述类而不是对象时,允许有很大的自由度。因此,子类不继承私有字段,但作为子类实例的对象当然包含私有字段。
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成员,那么事情就搞砸了。
私有类成员或构造函数只能在顶层类的主体(§7.6)中访问,该主体包含了成员或构造函数的声明。它不是由子类继承的。https://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.6
子类不继承父类的私有成员。但是,如果父类具有用于访问其私有字段的公共或受保护方法,则子类也可以使用这些方法。
这里的问题/答案中的大多数困惑都围绕着继承的定义。
显然,正如@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:)