superClass 的私有成员是否由子类继承... Java? [英] Are the private members of superClass inherited by a subClass... Java?

查看:43
本文介绍了superClass 的私有成员是否由子类继承... Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经历过这个:

子类是否继承私有字段?

但我还是一头雾水……

我说的是只继承而不是访问.我知道他们在课堂之外是不可见的.

I'm talking about inheriting only and not accessing. I know that they aren't visible out side class.

但是子类的对象是否在超类中拥有自己的私有成员的副本?

But does the subclass' object has it's own copies of those private members in super class?

例如...

class Base {
    private int i;
}

class Derived extends Base {
    int j;
}

现在,

Base b = new Base();
Derived d = new Derived();

int 的大小为 4

现在,

b 的大小是否为 4,d 的大小是否为 8

Will the size of b be 4 and size of d be 8

d 的大小也只有 4 吗?

size of d will also be 4 only ?

当然,当我说 b 和 d 时,我指的是堆上的对象,而不是引用.

Of course I'm talking about the objects on the heap when I say b and d and not the references.

更新:我刚刚阅读了 Kathy Sierra & 的 SCJP 书籍伯特......它说他们不是继承的......我发布了这个更新,因为仍然有很多人说是......

UPDATE: I just read in SCJP Book of Kathy Sierra & Bert... It says that they are NOT inherited.... I posted this update since still there are bunch of people saying yes...

推荐答案

是的,子类的实例将拥有父类私有字段的副本.

Yes, instances of the subclass will have copies of a private field of the parent class.

然而,它们对子类不可见,因此访问它们的唯一方法是通过父类的方法.

They will however not be visible to the subclass, so the only way to access them is via methods of the parent class.

这篇关于superClass 的私有成员是否由子类继承... Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆