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

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

问题描述

我已经完成了这个:

子类是否继承私有字段?

但我仍然感到困惑......

But I'm still confused...

我说的只是继承而不是访问。我知道他们在副课程中不可见。

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?

当我说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书& Bert ......它说它们不是继承的....我发布了这个更新,因为仍然有很多人说是......

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天全站免登陆