why private property can be inherited in this program?
peter tong
Ranch Hand
Joined: Mar 15, 2008
Posts: 234
posted
0
the code is placed in a physical file named classA.java.
property see in class A is private, so I suppose B cannot inherit this property, but when class C call b.getSee(), value "abc" can be retrieved, why would this happen?
peter tong wrote:
property see in class A is private, so I suppose B cannot inherit this property, but when class C call b.getSee(), value "abc" can be retrieved, why would this happen?
The class B did not inherit the private field named "see". It did however, inherit the public method named getSee().
peter tong wrote:
property see in class A is private, so I suppose B cannot inherit this property, but when class C call b.getSee(), value "abc" can be retrieved, why would this happen?
The class B did not inherit the private field named "see". It did however, inherit the public method named getSee().
Henry
of course I know B inherit getSee(), but getSee() get the value of property see, which B cannot inherit... or in fact, B has property see? if yes, why B has?
class scope is the solution of your problem...when we call getSee() method this is called directly because of pubic...but this public method checked by the compiler in parent class..so compiler call that method...after that cursor is in A class scope, so we can access private members easily in same class....
peter tong wrote:
or in fact, B has property see? if yes, why B has?
Remember that a B instance IS-A A instance. Remember that you constructed the super portion of the B instance during instantiation of B. So, yes, that property exists in the instance.
B class doesn't have see property...this is not inherited from super class that by compiler error bacause in B class there is no visibility of see property.
Thakur Sachin Singh wrote:B class doesn't have see property...this is not inherited from super class that by compiler error bacause in B class there is no visibility of see property.
Well, it does have the property. It just doesn't know it has it.Through reflection you can still get its value using an instance of class B.
Thakur Sachin Singh wrote:B class doesn't have see property...this is not inherited from super class that by compiler error bacause in B class there is no visibility of see property.
Well, it does have the property. It just doesn't know it has it.Through reflection you can still get its value using an instance of class B.
Hate to split hairs -- as it makes me feel like a lawyer. Technically, the property isn't part of Class B, as there is no definition of it. However, the property is part of all Class B instances, as instances have components defined by their super classes.