Access Specifier:

  • To Explicitly mention the way how the data (variables and methods of a class) available to outside.
  • Access specifier is something which mentions the way how the member of a class has to be available to anything outside the class.
  • Access specifiers not not applicable to the local variables.(present inside method).
  • Access specifiers are the keywords using which we can control the accessibility of the members of a class.
  • There are 4 different ways in which we can control the accessibility of the members of a class
  • Thus there are "4" access specifiers and they are,
  1. public
  2. private
  3. protected
  4. default (not a keyword) 
  • if we not specify any keyword that will be considered as default 

Public:

  • The access specifier "public" makes the member of a class available to any location outside of the class.
  • Thus anything mentioned as public would be available to any location outside the class.
  • Means if we have a class with public access specifier then we can use it in other packages also by importing the corresponding package.

  1. package package1;
  2. public Class A{ 
  3. public void show(){
  4. }
  5. public static void main (String args[]) {
  6. }
  7. }

  1. package package2;
  2. import package1.*;
  3. public Class B {
  4. public static void main (String args[]) {
  5. A a = new A();
  6.  a.show();
  7. }
  8. }

Private:

  • Private would not allow the member of class available to any location outside the class.
  • private members would be available only within the same class.
  • Private members of a class would not be available to anything outside the class under any condition.
  • Private members of a class would also be not available to the sub class objects

  1. package package1;
  2. Class A{ 
  3. private void show(){
  4. }
  5. public static void main (String args[]) {
  6. A a= new A();
  7. a.show();
  8. }
  9. }

Default:

  • Any member of class mentioned without any access specifier then its considers that as default .
  • Default will act as "public " within the same package and same path.
  • The same default would act as private outside the package.
  • Default members of any class would be available to anything within the same package and would not be available outside the package under any condition.

  1. package package1;
  2. Class A{ 
  3. void show(){
  4. }
  5. public static void main (String args[]) {
  6. A a= new A(); // works fine in same class
  7. a.show();
  8. }
  9. }



  1. package package1;
  2. Class B{ 
  3. public static void main (String args[]) {
  4. A a= new A(); // works fine in same package
  5. a.show();
  6. }
  7. }

  1. package package2;
  2. import package1.*;
  3. Class B{ 
  4. public static void main (String args[]) {
  5. A a= new A(); // gives an error
  6. a.show(); // gives an error
  7. }
  8. }

 

Protected:

  • protected will act as public within the same package and acts as private outside the package.
  • But protected will also act as public outside the package only with respect to sub class objects.
  1. package package1;
  2. public Class A{ 
  3. protected void show() {
  4. }
  5. }

  1. package package2;
  2. import package1.*;
  3. public Class B extends A { 
  4. public static void main (String args[]) {
  5. B b= new B();
  6. b.show();
  7. }
  8. }
  • Access specifiers are applicable to static inner class and member inner class.
  • Access specifiers are not applicable to anonymous inner class and local inner class

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

1 comments for Access Specifiers/modifiers in java

  1. nice explanation.thanks..

    as my understanding i can say that when we use public we can access any where within the class and outside the class and outside the package.when we use private it will be applicable for the class level, we cannot access it fr the subclasses.when we go with protected we can access the instance members through subclass using inheritance

    ReplyDelete

Select Menu