File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Inheritance Confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Inheritance Confusion" Watch "Inheritance Confusion" New topic
Author

Inheritance Confusion

Waheed Murad
Greenhorn

Joined: Jan 21, 2011
Posts: 7
Hi please see the code below,

-----------------------------------


-------------------------------------



------------------------------------




-----------------------------------------

Above are three classes. Please pay attention to class packages

i want to know that in Class Test3 why i am being able to access the test2.var1 variable. As once Class Test3 have inherited the var1 variable from Test1 Class
it should be like we have protected variable in Test3 Class. So why we are being able to access it from other package with a reference variable...(Test2) ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Waheed Murad wrote:So why we are being able to access it from other package with a reference variable...(Test2) ?

What "other package"? Remember that variables aren't inherited (so the title of your post is misleading). The variable is declared in package "general.pack1" and the code which attempts to use the variable is declared in that same package.

You could experiment with declaring your Test3 class to be in package general.package2 or in some completely different package.
Waheed Murad
Greenhorn

Joined: Jan 21, 2011
Posts: 7
Hi Paul Thanks for help,

I am using the Test2 Class reference(which is declared in general.package2 package) to access its protected instance variable from Class Test3 (which is declared in general.pack1 package).

Ok Class Test2 inheriting it from Test1 Class. But as i am using the object to refer it so shouldn't it be treat like it is declared in Class Test2. in which case it should give exception.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Waheed Murad wrote:Ok Class Test2 inheriting it from Test1 Class. But as i am using the object to refer it so shouldn't it be treat like it is declared in Class Test2. in which case it should give exception.


Well, the compiler disagrees with you. So are you right or is the compiler right? I know which the two I would believe...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Welcome to the Ranch

Please use the code button; since you are new I have edited your post and you can see how much better it looks. You can't use coloured text in code, however.
Waheed Murad
Greenhorn

Joined: Jan 21, 2011
Posts: 7
Paul :
I am not saying compiler is wrong... I was just wondering that there must be some OOPS reason, that's why compiler behaves like this ... Anyway thanks.

Campbell:
Will do the same why next time
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance Confusion