Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

que on voodooexam

 
sanjana narayanan
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This is one of the question in voodooexam
Suppose that you make a class file " MyProg . java " . You would like to add a method to this class file such that the method is available to only the subclasses of this class & also to other classes in th same package as the one in which " MyProg . java " is defined .
What access modifier should precede the method name to get such an effect ?
The given ans is protected but my answer is default(no access modifier) so that it can be accessed with the same class/package. what say?
-Sanjana
 
Vishy Karl
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you , Ans should be default ,(as it is given by other classes in same package
Happy Preps,
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I don't agree with you.
Because if the access modifier is default you won't
be able to inherit the method from subclasses that
are not in the same package. With protected as an
access modifier you can inherit the method from
a class outside the package.
Gian Franco
 
sanjana narayanan
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gian Franco Casula,

There is no mention abt classes/methods in outisde package. Please read the question again.
-Sanjana
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sanjana,
In the text:
Originally posted by sanjana narayanan:
Suppose that you make a class file " MyProg . java " . You would like to add a method to this class file such that the method is available to only the subclasses of this class & also to other classes in the same package as the one in which " MyProg . java " is defined .

I interpret the text as saying that the method should be available only to
1.) the subclasses of this class
2.) other classes in the same package
So for 1.) it does not say that these should be in the same package.
The text would have been less confusing I guess if it said:
...You would like to add a method to this class file such that the method is available only to other classes in the same package as the
one in which " MyProg.java " is defined and to the subclasses of this class...

What do you think?
Gian Franco
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic