• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

conflict: inheritance, package and import access

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Line 1 doesnt compile, I dont understand. Aren't protected members can be accessed through inheritance?

if package and import statements are omitted, it will compile fine and run fine.

All package and statement lines are properly declared. This is really confusing me.
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Protected methods are inherited by subclasses, so the subclass can invoke its own inherited test() method. However, this still doesn't give it the right to invoke its parent's test() method using a parent reference.

In other words, if ClassA has a protected method myMethod, a class in a different package can never invoke myMethod() using a ClassA reference. This is true even if the other class is a subclass of ClassA. Being a subclass only gives it the right to invoke myMethod() using a reference to its own class type--not the parent's type.

It works when you remove the package declarations because this then puts both classes in the same package--and protected methods are always visible to all classes in the same package.
[ October 29, 2007: Message edited by: Kelvin Lim ]
 
Ranch Hand
Posts: 153
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the first problem is, that the class Parent is not declared public. It is not visible in other packages and therefore can not be inherited from in other packages.
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marcus Didius Falco:
I think the first problem is, that the class Parent is not declared public. It is not visible in other packages and therefore can not be inherited from in other packages.



Good catch! I was blind-sided by the fact that Adam specifically said that "Line 1 doesn't compile". So, um, yeah, fix the class visibility first, and then read my earlier comment.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi adam,
In the above code you are trying to create an object of the superclass and access the members of the superclass but it is not possible, we can refer the super class members only through inheritance but not through the superclass's object from subclass.

Hope the above idea helps in clearing your doubt!
 
Popeye has his spinach. I have this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic