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 Protected Modifier Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Protected Modifier Problem" Watch "Protected Modifier Problem" New topic
Author

Protected Modifier Problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Whiz Lab mock Question




SuperclassX compiles fine

SubclassY gives follwing error:


C:\Users\maxaaaa\Documents\Javafiles\packageX\packageY>javac SubclassY.java
SubclassY.java:4: cannot find symbol
symbol: class SuperclassX
public class SubclassY extends SuperclassX
^
SubclassY.java:7: cannot find symbol
symbol : class SuperclassX
location: class packageX.packageY.SubclassY
SuperclassX obx=new SubclassY();
^
SubclassY.java:8: cannot find symbol
symbol : class SuperclassY
location: class packageX.packageY.SubclassY
SuperclassY oby=new SubclassY();
^
3 errors


how to compile SubclassY

is it possible for SubclassY to access protected member of SuperclassX via creating a object of SuperclassX in it.
is it possible for SubclassY to access protected member of SuperclassX via creating a object of SubclassY in it.

OCPJP 6.0 93%
OCPJWCD 5.0 98%
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
mohitkumar gupta wrote:
SubclassY.java:4: cannot find symbol
symbol: class SuperclassX
public class SubclassY extends SuperclassX
^


I don't get this error.
The probable mistake may be in the directory hierarchy, in import.
The compiler is not able to find the SuperclassX.class

mohitkumar gupta wrote:
SubclassY.java:7: cannot find symbol
symbol : class SuperclassX
location: class packageX.packageY.SubclassY
SuperclassX obx=new SubclassY();
^

The same reason...

mohitkumar gupta wrote:
SubclassY.java:8: cannot find symbol
symbol : class SuperclassY
location: class packageX.packageY.SubclassY
SuperclassY oby=new SubclassY();
^

SuperclassY is not defined.
Probably a typo in the code, I guess you meant SuperclassX


OCPJP6
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
mohitkumar gupta wrote:
is it possible for SubclassY to access protected member of SuperclassX via creating a object of SuperclassX in it.

No, it's not possible.
A subclass from different package can access protected member only through the subclass object, and not the superclass object.

mohitkumar gupta wrote:
is it possible for SubclassY to access protected member of SuperclassX via creating a object of SubclassY in it.

Yes, that's the way protected members of superclass are accessed in the subclass from different package.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

mohitkumar gupta wrote:
is it possible for SubclassY to access protected member of SuperclassX via creating a object of SuperclassX in it.
is it possible for SubclassY to access protected member of SuperclassX via creating a object of SubclassY in it.


You can't access a Super class members with the Super class objects in the Sun class, which is in different package! protected == inheritance in the case of different packages!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Protected Modifier Problem