Home » Core Java » Access Modifer in Java

Access Modifer in Java

There are  4 types of Access Modifier

1 : public
2 : protected
3 : default
4 : private
 

 
public access modifier :

public is a  keyword which is used with classes, methods and variables .If used with a class , will allow classes in the same package or in any other package of your java project to access it without any restriction. The same is applicable to variables and methods if they are declared public.

protected access modifier :
protected keyword is used with methods and variables and inner classes. If a method or variable is declared protected, only classes within the same package and “child” classes within different package can access them.The same is applicable to variables and inner classes if they are declared protected.

default access modifier :
This is often called as package level access.If no modifier is used with a class, method or variable, then  all  these have default access. This means only the classes in the same package can access them.

private access modifier :
 private keyword is used with methods,variables and inner classes.Even if the class is defined as public ,if we make a variable or method as private inside the class,then they are accessible only within the class. So private access modifier makes these as not share-able across classes.

From the above explanation, the order of access from highest to lowest is
public ->protected->default->private

 


Leave a comment

Your email address will not be published. Required fields are marked *

1 × four =