Access modifiers define the visibility of the class. If no keyword is mentioned then that is default access modifier. Four access modifiers in java include public, private, protected and default. Private and Protected keywords cannot be used for classes and interfaces. Hope you enjoyed the whole bundle of tutorials in this JAVA series. Java Access Modifiers One of the main principle of Object Oriented Programming is 'information hiding', which means that objects don't reveal all of their details to the outside world. Just like other object-oriented programming languages, Java has access modifiers to restrict access to members of classes, traits, objects and packages.
As the name suggests access modifiers in Java helps to restrict the scope of a class, constructor , variable , method or data member. There are four types of access modifiers available in java:
- Default – No keyword required
- Private
- Protected
- Public
- Default: When no access modifier is specified for a class , method or data member – It is said to be having the default access modifier by default.
- The data members, class or methods which are not declared using any access modifiers i.e. having default access modifier are accessible only within the same package.
In this example, we will create two packages and the classes in the packages will be having the default access modifiers and we will try to access a class from one package from a class of second package.package
p1;
//Class Geeks is having Default access modifier
{
{
}
//using class from different package with
package
p2;
class
GeekNew
public
static
void
main(String args[])
//accessing class Geek from package p1
}
Output: - Private: The private access modifier is specified using the keyword private.
- The methods or data members declared as private are accessible only within the class in which they are declared.
- Any other class of same package will not be able to access these members.
- Top level Classes or interface can not be declared as private because
- private means “only visible within the enclosing class”.
- protected means “only visible within the enclosing class and any subclasses”
Hence these modifiers in terms of application to classes, they apply only to nested classes and not on top level classes
In this example, we will create two classes A and B within same package p1. We will declare a method in class A as private and try to access this method from class B and see the result.//using class from different package with
package
p1;
class
A
private
void
display()
System.out.println(
'GeeksforGeeks'
);
}
class
B
public
static
void
main(String args[])
A obj =
new
A();
//trying to access private method of another class
}
Output: - protected: The protected access modifier is specified using the keyword protected.
- The methods or data members declared as protected are accessible within same package or sub classes in different package.
In this example, we will create two packages p1 and p2. Class A in p1 is made public, to access it in p2. The method display in class A is protected and class B is inherited from class A and this protected method is then accessed by creating an object of class B.//protected modifier
public
class
A
protected
void
display()
System.out.println(
'GeeksforGeeks'
);
}
//protected modifier
import
p1.*;
//importing all classes in package p1
//Class B is subclass of A
{
{
obj.display();
Output: - public: The public access modifier is specified using the keyword public.
- The public access modifier has the widest scope among all other access modifiers.
- Classes, methods or data members which are declared as public are accessible from every where in the program. There is no restriction on the scope of a public data members.
//public modifier
public
class
A
public
void
display()
System.out.println(
'GeeksforGeeks'
);
}
import
p1.*;
{
{
obj.display();
}
Output: - If other programmers use your class, try to use the most restrictive access level that makes sense for a particular member. Use private unless you have a good reason not to.
- Avoid public fields except for constants.
Important Points:
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Java provides a number of access modifiers to set access levels for classes, variables, methods, and constructors. The four access levels are −
- Visible to the package, the default. No modifiers are needed.
- Visible to the class only (private).
- Visible to the world (public).
- Visible to the package and all subclasses (protected).
Default Access Modifier - No Keyword
Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.
A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public.
Example
Variables and methods can be declared without any modifiers, as in the following examples −
Private Access Modifier - Private
Methods, variables, and constructors that are declared private can only be accessed within the declared class itself.
Private access modifier is the most restrictive access level. Class and interfaces cannot be private.
![Java Java](/uploads/1/2/6/2/126256122/684435052.png)
Variables that are declared private can be accessed outside the class, if public getter methods are present in the class.
Using the private modifier is the main way that an object encapsulates itself and hides data from the outside world.
Example
The following class uses private access control −
Here, the format variable of the Logger class is private, so there's no way for other classes to retrieve or set its value directly.
So, to make this variable available to the outside world, we defined two public methods: getFormat(), which returns the value of format, and setFormat(String), which sets its value.
Public Access Modifier - Public
A class, method, constructor, interface, etc. declared public can be accessed from any other class. Therefore, fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe.
However, if the public class we are trying to access is in a different package, then the public class still needs to be imported. Because of class inheritance, all public methods and variables of a class are inherited by its subclasses.
Example
The following function uses public access control −
The main() method of an application has to be public. Otherwise, it could not be called by a Java interpreter (such as java) to run the class.
Protected Access Modifier - Protected
Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.
The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.
Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it.
Example
The following parent class uses protected access control, to allow its child class override openSpeaker() method −
Here, if we define openSpeaker() method as private, then it would not be accessible from any other class other than AudioPlayer. If we define it as public, then it would become accessible to all the outside world. But our intention is to expose this method to its subclass only, that’s why we have used protected modifier.
Access Control and Inheritance
The following rules for inherited methods are enforced −
- Methods declared public in a superclass also must be public in all subclasses.
- Methods declared protected in a superclass must either be protected or public in subclasses; they cannot be private.
- Methods declared private are not inherited at all, so there is no rule for them.