Public vs Package Access Modifiers in Java


The public and package 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 package 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 work 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.

Package Access Modifier

It is also known as the default access modifier. When we don’t specify any access specifier explicitly to classes and methods, the java compiler automatically considers it as a default or package member. We can access these members inside sub classes as well as other classes within the same package.

A few examples of package access modifier −

int i1 = 108;
double d2 = 6.55;

Here, variables are declared as default or package.

Example

The following example illustrates how package members work in Java.

public class Overflw {
   static void methodA(int data1) { 
      // default method 1
      data1++;
      methodB(data1); 
      // calling methodB
   }
   static void methodB(int data1) {   
      // default method 2
      data1++;
      int data2 = 5; 
      // default member variable
      int mult = data1 * data2;
      System.out.println("Value of data1 and data2 multiplication is: " + mult);
   }
   public static void main(String []args) {
      int data1 = 0;  
      // variable with default modifier
      methodA(data1); 
      // calling methodA
   }
}

Output

Value of data1 and data2 multiplication is: 10

We have created two parameterized user defined methods named ‘methodA’ and ‘methodB’. In the main method, we have declared and initialized an integer variable ‘data1’ to 0 and then, passed it as an argument to the ‘methodA’. Now, the ‘methodA’ is called the ‘methodB’ with the incremented value of ‘data1’. Inside ‘methodB’ we have calculated multiplication and printed the result.

Public vs Package access modifiers

From the above discussion, we can conclude the following differences between Public and Package access modifiers &miuns;

Public

Package

We need to use the keyword ‘public’ to specify that a member is made public.

It does not require any specific keyword.

Public members are accessible within the package as well as outside the package.

package members are accessible only to the same package.

It does not provide any restrictions.

It is more restricted than the public access modifier.

Conclusion

We started this article by defining public and package access modifiers and in later sections, we have explained them in detail with respective examples. In the end, we have discussed how they are different from each other.

Updated on: 16-May-2023

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements