• Post Reply Bookmark Topic Watch Topic
  • New Topic

why private property can be inherited in this program?  RSS feed

 
peter tong
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
peter tong
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
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?
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to learn flow of program

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....
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

As for why has, you have an example of it.

Henry
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes instance is also the reason.
 
peter tong
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
So, yes, that property exists in the instance.
Henry


to make thing clear (or worse), I add the following function in class B


it return compile error!! said A.see is not visible... so B has property see but cannot access it even in Class B?
 
Thakur Sachin Singh
Ranch Hand
Posts: 248
Hibernate Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
peter tong wrote:so B has property see but cannot access it even in Class B?

Exactly. That's what private is for - blocking visibility.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
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.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!