- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Public vs Private Access Modifiers in Java
The public and private access modifiers define scope of a variable, class and method means they determine how a member of a class or method can be accessed. The modifiers are attached to the members at the time of declaration. We know that these access modifiers play a major role in Java oops concepts like encapsulation, polymorphism and inheritance. It helps in preventing misuse of functionalities provided by a member. In this article, we will understand public and private access modifiers in Java through example programs.
Access Modifiers in Java
Public Access Modifier
Java does not restrict the accessibility of public members. Anything declared public can be accessible everywhere means we can access them within the class as well as outside the class and also within the package as well as outside the package. You may have noticed that the main() method in Java is always defined as public so that the JVM that exists outside any scope of the current program can call it.
A few examples of public access modifier −
public int i1 = 108; public double d2 = 6.55;
Here, variables are declared as public.
Example
The following example illustrates how package members works in Java.
class Pack { public void prnt() { // method declared as public String msg = "I am inside a public method"; System.out.print(msg); } } public class ClassShow extends Pack { // public child class public static void main(String args[]) { // creating object of child class ClassShow obj = new ClassShow(); // method calling through object of child class obj.prnt(); } }
Output
I am inside a public method
In the above code, the class ‘Pack’ is parent class of ‘ClassShow’. In the parent class, we have declared a public method named ‘prnt()’ to print a simple message. In the main() method of child class, we have defined an object of child class ‘ClassShow’ to call the public method ‘prnt()’. Here, the child class is also public.
Private Access Modifier
When we declare a class member as private then, it becomes non accessible to any code outside its class, including its subclasses. We can achieve higher level of encapsulation by using private keyword.
A few examples of private access modifier:
private int mobNo = 982266; private double income = 35005.55;
Here, variables are declared as private.
Example
The following example illustrates what if we declare a method as private in Java.
class Pack { private void prnt() { // declaring a method private String msg = "I am inside a private method"; System.out.print(msg); } } public class ClassShow extends Pack { public static void main(String args[]) { // creating object of child class ClassShow obj = new ClassShow(); // method calling through object of child class obj.prnt(); } }
Output
ClassShow.java:12: error: cannot find symbol obj.prnt(); ^ symbol: method prnt() location: variable obj of type ClassShow 1 error
Here we modified the code of previous example, we changed the public access modifier to private. We got a compile time error because we tried to access a private method in subclass.
Public vs Private access modifiers
From the above discussion, we can conclude the following differences between Public and Private access modifiers:
Conclusion
We started this article by defining public and private access modifiers and in later sections, we have explained them in detail with respective examples. In the end, we have discussed a few differences between them.
Public |
Private |
---|---|
We need to use the keyword ‘public’ to specify that a member is made public. |
We need to use the keyword ‘private’ to specify that a member is made private. |
We can define any class as public. |
We can’t define a top class as private but an inner class could be. |
Public members are accessible within the package as well as outside the package. |
Private members are not accessible even within the same package. |
They are accessible to inherited classes. |
They are not accessible to inherited classes. |
It is applicable to both top level and member level. |
It is applicable to member level only. |