Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access Modifiers

 
rajani peddi
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Top level classes can be declared as public or they can be default. They can't be protected. Why is this so? When they can have default access modifier(friendly) which is more restricted than protected why can't they have their access modifier as protected?
rajani
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am moving this to Programmer Certification Study as it is more suited to that area as opposed to Mock exam erratas.
Bill
 
rajani peddi
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks bill...i posted it in the exam erratas by mistake
 
Edy Yu
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"protected" can only be used to methods, variables and inner classes.
 
Oliver Grass
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajani,
i don't know if you're satisfied with this answer but this behavior is defined in the Language specification. You have to accept this...
hope that helps
cheers
Oliver
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if a class 'x' of package 'p' has a default access modifier then there's no point having a protected method 'm' with 'x'
because in another package we can't access class 'x' so how can we access 'm' .So my conclusion is that
a class should be public(although not enforced by java) if its is declaring some protected members.
please someone tell me if my conclusion is wrong.

 
Sathvathsan Sampath
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class needs to access within a package or outside it.
So, default access will do for package access & public will do for outside package access. So, under what cicumstance would we need a protected modifer for??
Also, nasir, I do not agree with your view. Please have a loo at this and I hope u'll get clarified http://www.javaranch.com/ubb/Forum24/HTML/003727.html
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sathvathsan
you realy helped by giving me old post by sampaths77.
What I extracted from that is
Even a class 'x' has a default access modifier its public or protected members can be accssed by another class in another package if x extends a public class .
 
rajani peddi
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all...my doubt is cleared
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic