OOP and inheritance of private fields

In C# the spec says:

Instance constructors, destructors, and static constructors are not inherited, but all other members are, regardless of their declared accessibility (§3.5). However, depending on their declared accessibility, inherited members might not be accessible in a derived class.

so private fields are inherited into a derived class but they are not accessible.

However the Java spec says:

Only members of a class that are declared protected or public are inherited by subclasses declared in a package other than the one in which the class is declared.

so private fields are not inherited into a derived class.

And what does explain the OOP theory? Is correct C# or Java designers?

A bit confused on this aspect.

PS I haven't C++ experience? What does C++ designers says on this?


Well, the C# version is more-clear because even in Java, private fields will be available as part of the child object but they will no be directly accessible unless you have a public getter in the parent class to get its value.

You can actually use reflection to make private fields (of the parent ) accessible and read their values directly.


Keeping the things simple and clean, taking some part of quote you mentioned for c++

However, depending on their declared accessibility, inherited members might not be accessible in a derived class.

The same thing happens in Java as well. You can't access private fields in Java too.

There are methods around them to access which is another story (encapsulation)


Answering at least the C#/Java-part as I don´t know much on C++.

However, depending on their declared accessibility, inherited members might not be accessible in a derived class.

The C#-spec seems to be a bit more meaningful, although Java handles private mebers the same way. The private members from base-class also exist in Java (however they are not inherited, see are private fields inherited in Java?). As you cannot access them this is not usefull anyway. The internals however are of course initialized by setting the private members also. So instances of your derived class of course have all the private members of the base-class, inheritance however means any kind of polymorphism which implies you might be able to override (assuming your member is not static or final/selead ) them which makes no sense at private members.

So coming to the chase there should be no need for accessing the internals at all, neither in C# nor in Java nor anywhere else. Simply assume your derived instances get all the base-members fully-initialized and do your actual work.

Relating to what you call "OOP-theory" I doubt there is a cleaner answer as for this principle (which is implemented in different ways in the mentioned languages)private members have no relevance at all. OOP merely handles the interactions between objects with their surrounding not their actual internals.

链接地址: http://www.djcxy.com/p/60260.html

上一篇: 用于对象初始化的Delphi类构造函数

下一篇: OOP和私人领域的继承