Access Specifiers in Java

 Access Specifiers

In Java, there are 4 types of Access Specifiers:

   
A. Class Level:
    1. If class is declared as public then that class is visible to all the classes everywhere inside that project.
    2. If the class is declared as default (package private) then that is visible to the classes which are present in the same package.

B. Member Level:
    At member level we can use all the four access modifiers.

1. Public:
  •  If a method, variable or constructor is declared as public then we can access them from anywhere.
  • When we are accessing the public member its class also should be public otherwise will be getting compile time error.
2. Default:
  • If a method, variable or constructor is declared as default then we can access them from current package only. So it is also called "PACKAGE-PRIVATE".
3. Private: 
  • If a method, variable or constructor is declared as private then we can access them in the current class only.
  • Private is the most restricted access modifiers.
  • If a constructor is declared private we can't create a object for that class in other classes.
4. Protected:
  • If a method, variable or constructor is declared as protected then we can access them with in the current package.
  • We can use protected members outside the package only in child class, and we can access them by using child class reference only not from parent class reference.

                        






Comments

Popular posts from this blog

OOP's Concepts

Basics about Core Java

Data types and Variables