Java Access Modifiers- public, protected, private and default
|Access specifiers also known as Access modifiers are keywords that enable a user to control the access limits of a certain class, method, constructor, inner classes or even variables.
There are four access modifiers in Java:
- Public
- Protected
- Private
- Default
Privacy and access control is an important aspect of any programming language that limits the usage of any particular entity or its attributes(variables) and features(methods) by the external as well as internal entities.
The figure below shows the visibility of the members of class A in other class within the same package and outside the package. If an access modifier is written inside a package then it means it is available to all the entities of that package.
Let’s discuss each access modifier in detail-
The Public Access Modifier
The ‘public‘ access modifier makes the variable, constructor, interface or method accessible in any of the classes. Any class declared as ‘public‘ can have its public members accessed anywhere inside Java. It is the widest scope available in Java. But any java class declared as ‘public‘ still needs to be imported in another package. In order to make any class, method, interface or variable as ‘public‘ we just need to write the ‘public‘ keyword in the declaration statement. The program below shows the use and scope of the ‘public‘ members.
Unable to retrieve the Code part. Please reload again. Notify us if the problem still persists. Till we work on this you can view code on URL below. Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/AccessSpecifier/PublicDemo.java
Output:-
Inside class Demo
The value of a is : 5
Question – Do you wonder why the main method is public and static in the program?
Answer – You will witness this in every Java program as this is the entry point for the JVM to launch the program. It is public so that it can be accessed from everywhere and it is static so that it can be called without creating any object.
The Protected Access Modifier
Any member declared as ‘protected‘ can be accessed within any class in the same package and also within any subclass of the class where the member is declared. The subclass can be inside another package as well. Interfaces and classes cannot be declared as ‘protected‘. But methods, constructors, inner classes and variables can be declared ‘protected‘. The members(i.e., methods and variables) inside the interface cannot be declared as ‘protected‘.
Unable to retrieve the Code part. Please reload again. Notify us if the problem still persists. Till we work on this you can view code on URL below. Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/AccessSpecifier/TestProtected.java
Output:-
The value of a is : 5
The Private Access Modifier
Any member declared as ‘private‘ can only be accessed within the class where it is declared. Any class or interface cannot be declared as ‘private‘. The ‘private‘ access specifier is the most restricted access specifier.
Unable to retrieve the Code part. Please reload again. Notify us if the problem still persists. Till we work on this you can view code on URL below. Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/AccessSpecifier/TestPrivate.java
In this Code, we see that a private member of the class Demo2 is being accessed inside a subclass and also a class within the same package so we get an error in line 8 and line 18.
Output:-
TestPrivate.java:8: error : a has private access in Demo2
System.out.println("The value of a is : "+a);//accessing variable a inside a subclass. This line will show an error message.
TestPrivate.java:18: error : a has private access in Demo2
System.out.println("The value of a is : "+obj.a);//accessing variable a inside another class of the same package. This line will show an error message.
The Default Access Modifier
The class or its members that do have any of the three access specifiers mentioned in its declaration falls under the default access modifier or the package access specifier. These members are accessible within any class present under the same package.
Unable to retrieve the Code part. Please reload again. Notify us if the problem still persists. Till we work on this you can view code on URL below. Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/AccessSpecifier/TestDefault.java
Output:-
The value of a is : 5
The value of b is : 10
An investment in knowledge always pays the best interest. Hope you like the tutorial. Do come back for more because learning paves way for a better understanding.
Do not forget to share and Subscribe.
Happy coding!! 🙂
Woow amazing