7 Difference between Public, Private and Protected in Java | Code Examples

7 Difference between Public, Private and Protected in Java | Code Examples

In Java, we come across a word called access modifier, which helps us classify the accessibility to any class, method, or variable.

The polarity amid these access modifiers surfaces from their ability to confine access to a class, method, or variables.

There are basically three types of access modifies in Java.

  • public
  • private
  • protected

Let’s see how these class modifiers are different from each others.

Difference between Public, Private and Protected in Java

We are going to see the differences by considering various points. Let’s check one-by-one.

1. Level of Accessibility

The ‘public’ is the least restrictive access modifier,  ‘private’ is the most restrictive access modifier, and ‘protected’ lies in between.

Anything public is accessible to anywhere, anything private is merely accessible to the class they’re declared and anything protected is accessible outside the package but only to child classes.

2. Provision of Encapsulation

Another difference between these modifiers is that they bring in Encapsulation. It is one of the important concepts in Object-Oriented programming in Java.

Encapsulation says hide from view anything which varies and that is why we hide implementations.

The ‘public’ keyword provides the least possible level of Encapsulation and the ‘private’ modifier provides the very best level of Encapsulation in Java.

3. Usage of Modifier

We can use public modifiers with top-level class but cannot make a top-level class private in Java. And on that note, we must not miss the fact that both public and private modifiers can be used with the classes like nested and inner classes.

4. Provision of Method Overriding

Private methods cannot be overridden while public and protected methods can be overridden.

Note: Don’t get confused between method overloading and overriding. You can check the difference between them.

Java code Examples for Class Modifiers

Before wrapping up, let us look into a few basic examples of the 3 access modifiers in Java.

Before going to these examples, I’m expecting you to know how to create classes and objects in Java.

5. Example code snippet for ‘public’ access modifier:

In this example, two classes ‘Demo1’ and ‘Demo2’ will be used to demonstrate the accessibility limits of the ‘public’ access modifier.

Both classes are in different packages. Since class Demo1 is set to be public, it can be accessed outside the class and package as well.

package com.example;
public class Demo1 {
	public void test() {
		System.out.println(“Welcome to CSEStack!”);
	}
}

package com.test;
public class Demo2 {
	public static void main(String[] args) {
		Demo1 demo = new Demo1();
		demo.test();
	}
}

Output:

The expected output of the above code would be :

Welcome to CSEStack!

6. Example code snippet for ‘private’ access modifier:

In this example, two classes ‘Demo1’ and ‘Demo2’ will be used to demonstrate the accessibility limits of ‘private’ access modifier.

The ‘Demo1’ class consists of one private variable and method each. While we try to access the variable and method through ‘Demo2’ class, a compile-time error would occur.

package com.example;
class Demo1 {
	private var =25;
	private void test() {
		System.out.println(“Welcome to CSEStack!”);
	}
}

package com.test;
public class Demo2 {
	public static void main(String[] args) {
		Demo1 demo = new Demo1();
		System.out.println(demo.var);
		demo.test();
	}
}

The above code snippet would give us a compile time error while we try to execute it.

7. Example code snippet for ‘protected’ access modifier:

In this example, two classes ‘Demo1’ and ‘Demo2’ will be used to demonstrate the accessibility limits of ‘protected’ access modifier.

Both classes have been created in two different packages.

‘Demo1’ class is set to be public so that it can be accessed from anywhere.

The method test() in ‘Demo1’ class is made protected, so to access it we need to inherit Demo2 class from Demo1 class.

package com.example;
public class Demo1 {
	protected void test() {
		System.out.println(“Welcome to CSEStack!”);
	}
}

package com.test;
public class Demo2 extends Demo1 {
	public static void main(String[] args) {
		Demo2 demo = new Demo2();
		demo.test();
	}
}

Output:

The expected output of the above code would be :

Welcome to CSEStack!

And that’s it! In this tutorial, we had a short ride to a few major difference between public, private and protected in Java access modifiers and later we had seen a sneak peek of their usage with the help of a quick basic example for each type of access modifier.

Happy Learning!

Leave a Reply

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