File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes conflict: inheritance, package and import access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "conflict: inheritance, package and import access" Watch "conflict: inheritance, package and import access" New topic
Author

conflict: inheritance, package and import access

adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186



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.


boolean b = true;<br />System.out.println ("I believe in Java.<br />Java will make my dream come " + b);
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
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 ]

SCJP 5.0
Markus Schmider
Ranch Hand

Joined: Feb 25, 2007
Posts: 100
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

Joined: Oct 20, 2007
Posts: 513
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.
srk bhushan
Greenhorn

Joined: Oct 26, 2007
Posts: 3
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: conflict: inheritance, package and import access
 
Similar Threads
Doubts on Protected Members
Kindly explain this code from SCJP faq
Protected
Doubt in protected access
scope question