• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2021
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools

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

Pranati Paidipati/6558/0
CodeJAVA
YouTube CSEstack

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!

Java Oops Concepts
Pranati Paidipati
I am a graduate in computer science with a creative bent of mind for writing content. With great gusto, I enjoy learning new things. I trail in database management system, and object-oriented programming languages like Java, C/C++.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Basic Java Tutorial

  1. Java- Tutorial Overview
  2. Java- Features & Characteristics
  3. Java-  Installation & Setup
  4. Java- Hello, World Program!
  5. Java- JDK vs JVM vs JRE
  6. Java- Data Types & Variables
  7. Java- String & its Methods
  8. Java- Different Operators Types
  9. Java- Flow Control Statements
  10. Java- Exception Handling
  11. Java- ‘throw’ vs ‘throws’ Keyword
  12. Java 12- New Advanced Features

Java OOPs concepts

  1. Java- OOPs Introduction
  2. Java- Classes & Objects
  3. Java- Constructor & Overloading
  4. Java- Method Overload vs Override
  5. Java- Access Modifiers
  6. Java- Abstraction
  7. Java- Inheritance
  8. Java- Interfaces
  9. Java- Nested Inner Classes

Java Advanced

  1. Java- Applet vs Application
  2. Java- HashMap Collections
  3. Java- ArrayList
  4. Java- HashSet
  5. Java- HashMap vs HashSet

Java Exercise

50+ Java Coding Questions [Practice]

Java Projects

Patient Billing Software

© 2021 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard