It's not a secret anymore!
The moose likes Java in General and the fly likes why private property can be inherited in this program? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "why private property can be inherited in this program?" Watch "why private property can be inherited in this program?" New topic
Author

why private property can be inherited in this program?

peter tong
Ranch Hand

Joined: Mar 15, 2008
Posts: 234


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

Joined: Sep 28, 2004
Posts: 16695
    
  19

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
peter tong
Ranch Hand

Joined: Mar 15, 2008
Posts: 234
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

Joined: Jun 15, 2010
Posts: 209

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....


my blog SCJP 6- 91%, IBM DB2, IBM RAD Certified
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19

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

Joined: Jun 15, 2010
Posts: 209

yes instance is also the reason.
peter tong
Ranch Hand

Joined: Mar 15, 2008
Posts: 234
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

Joined: Jun 15, 2010
Posts: 209

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

Joined: Oct 27, 2005
Posts: 19216

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.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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

Joined: Sep 28, 2004
Posts: 16695
    
  19

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why private property can be inherited in this program?
 
Similar Threads
Argument problem
looking at some code ('this', and a method)
sequence confusion
Different output without the parantheses