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,
- public
- private
- protected
- 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.
- package package1;
- public Class A{
- public void show(){
- }
- public static void main (String args[]) {
- }
- }
- package package2;
- import package1.*;
- public Class B {
- public static void main (String args[]) {
- A a = new A();
- a.show();
- }
- }
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
- package package1;
- Class A{
- private void show(){
- }
- public static void main (String args[]) {
- A a= new A();
- a.show();
- }
- }
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.
- package package1;
- Class A{
- void show(){
- }
- public static void main (String args[]) {
- A a= new A(); // works fine in same class
- a.show();
- }
- }
- package package1;
- Class B{
- public static void main (String args[]) {
- A a= new A(); // works fine in same package
- a.show();
- }
- }
- package package2;
- import package1.*;
- Class B{
- public static void main (String args[]) {
- A a= new A(); // gives an error
- a.show(); // gives an error
- }
- }
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.
- package package1;
- public Class A{
- protected void show() {
- }
- }
- package package2;
- import package1.*;
- public Class B extends A {
- public static void main (String args[]) {
- B b= new B();
- b.show();
- }
- }
- 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
nice explanation.thanks..
ReplyDeleteas 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