Forums Register Login

conflict: inheritance, package and import access

+Pie Number of slices to send: Send



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.
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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!
A magnificient life is loaded with tough challenges. En garde tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 833 times.
Similar Threads
Doubt in protected access
scope question
Kindly explain this code from SCJP faq
Protected
Doubts on Protected Members
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:15:14.