• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access Specifiers - a Summary

 
krishnaraj kalimuthu
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Access Control :
----------------
- Access modifiers are public, protected, private.
If nothing is provided,
default - 'friendly' - package level -
permissions are given for class/members.
public permissions are given for interface members
note : friendly is not a keyword

- Accessibitlity is different in each of the following
- within the class,
- another class in the same package,
- sub class in same package,
- another class in the another package,
- sub class in another package.
- Therefore, there are
- four types of access permissions and
- five places to test the accessibility.
- Totally 20 combinations.
- Accessibility with in the class :
- private - available
- friendly - available
- protected - available
- public - available
- Accessibility with in sub class in same package :
- private - not available
- friendly - available
- protected - available
- public - available
- Accessibility with in another class in same package :
- private - not available
- friendly - available
- protected - available
- public - available
- Accessibility with in sub class in another package :
- private - not available
- friendly - not available
- protected - available
- public - available
- Accessibility with in another class in another package :
- private - not available
- friendly - not available
- protected - not available
- public - available

- Important :
- public is more visible than protected,friendly,private
- proctected is more visible than friendly,private
- friendly is more visible than private
- private is no more visible than anything.
- while overriding,
- narrowing accessibility is not allowed.
- windening accessibility is legal.
- narrowing accessibility is
public -> protected -> friendly -> private
- widening accessibility is
private -> friendly -> protected -> public

- Access Modifier Applicability :
- Access modifiers are only applicable to
classes and class members and not method variables.

- classes :
- package level class should only be public or friendly.
- inner classes can be public, protected, friendly, and private.
- constructors :
public, protected, friendly, and private
- methods
public, protected, friendly, and private
- variables
public, protected, friendly, and private
- Class with only private constructor(s)
- this class can not be instantiated.

 
Frank Carver
Sheriff
Posts: 6920
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A very interesting summary, but not really apropriate to the "performance" forum. I've moved it to the "Java in General (Beginner) forum instead.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic