• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Access Modifiers

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks bill...i posted it in the exam erratas by mistake
 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"protected" can only be used to methods, variables and inner classes.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks all...my doubt is cleared
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic