File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance Confusion

 
Waheed Murad
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20171
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20171
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic