• 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

More / Less Restricted question on Modifiers.

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,

I am giving SCJP 1.5 on 19th March and i am quite confident on passing. I always get confused on access modifiers only in terms of access. Meaning, i have seen many exam questions saying "This can use less restricted, but not more restrictive", the same stands for Exceptions too. Overriding method should not throw broader or newer exceptions. This is OK for me.

But less / more restrictive for modifiers always bowls me over and i find the question tough to clear. Would request you'll to please explain in simple language what does it mean , please help me immediately as i have only 48 more hours now left for the exam !

Please give me some tips for exam also.

Thanks,
Yogendra N Joshi.
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With exceptions more restrictive or 'narrower' exceptions mean simply sub-types of the original exception. For example if a method throws a general Exception, then an overriding method may throw a narrower exception which is a subtype of the Exception class such as IOException for example, as IOException extends Exception. (Though I have used this as a very general example, it would not be good design to throw general exceptions as all Checked exceptions below that could be thrown also!).

 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, here is what I think you are asking. My terminology may not be technical but I am trying to get an idea across in plain language so please forgive that.

The two easiest to understand are public and private.

Public means any thing ( any class, any object ) can get to it. THIS is the easiest.

Private means only this one class can get to it...THIS is the MOST restrictive.

Protected means that anything that can subclass the class can get to it. It does not matter whether or not you are in the same package.

Default (which in Java is what you get when you do not type a modifier) means that anything inside the same package can get at it and nothing else.

So,

Easiest: public
next: protected
next: package(default)
Strictest: private

protected is less restrictive than package because, if the class and members being subclassed are protected they can be inherited from any package.
If a class is protected but has members with package access, then the class may be inherited from any package BUT the package access members will not be available.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic