• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Inheritance Confusion

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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) ?
 
Sheriff
Posts: 28417
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 28417
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Marshal
Posts: 80777
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic