Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP and inheritance of private fields [closed]

Tags:

java

c++

c#

oop

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.

P.S. I haven't C++ experience? What does C++ designers says on this?

like image 961
xdevel2000 Avatar asked Feb 16 '16 09:02

xdevel2000


1 Answers

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.

like image 71
TheLostMind Avatar answered Sep 22 '22 12:09

TheLostMind