• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Protected Modifier Problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic