• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Classes » The four Ps of protection in java
Next →
← Prev

The four Ps of protection in java

By Dinesh Thakur

Java provides four levels of protection for methods and instance variables: public, private, protected and package. Before applying protection levels to a program, one should know what each form means and understand the fundamental relationships that a method or variable within a class can have to the other classes in the system.

Package Methods and variables with package protection are visible to all other classes in the same package but not outside that package. Most of the time, the programmer needs to be more explicit when protection is defined for methods and variables of a class. Package protection is the default level of protection. Package protection is not an explicit modifier; it can be added to the definition of a method or variable. Private The keyword private limits the visibility of methods and instance variables to the class in which they are defined. For example, private instance variable can be used by methods inside the same class but cannot be seen or used by any other class or object. In addition, neither private variables nor private methods are inherited by sub-classes.

Private protection ensures that methods and variables are accessible only to other methods in the same class. To create a private method or instance variable, add the private modifier to its definition as shown in Program

Implementing private access specifier for variables and methods.

class PrivateClass {
 private String name = "Java";
 private int age = 20;
 public void show() {
   System.out.println("Name" +name);
   System.out.println ("Age"+age);
 }
 private void show1() {
   System.out.println(" Name" +name);
   System.out.println("Age"+age);
 }
}
 class Private Demo {
   public static void main(String args[]) {
     PrivateClass p1 = new PrivateClass();
     p1.show(); //accessible
     //p1.show(); //not accessible as show1() is declared as private method
   }
 }

The output of Program is given below:

Name Java 
Age 20

Though the variables Name and Age in Program  are declared as private, all instance variables are private by default. Here, the method show() is public and hence it can be accessed from the method main(); however, if we include p1.show1() (as mentioned in the last comment line) in the program, compilation of the program will give an error because the method show1() is private and hence cannot be accessed from the method main()·

Public The opposite of private, and the least restrictive form of protection, is public. A method or variable that is declared with the modifier public is accessible to the class in which it is defined and all the sub-classes of that class, as well as all the classes in the package and any other classes outside that package.

In many ways, public is similar to the default package. Both allow methods and variables to be accessed by other classes in the same package. The difference occurs when the package is created. Variables and methods with package protection can be used in classes that exist in the same package. But if the class is imported from outside the package, those methods and variables will not be accessible unless they have been declared public. The syntax for declaring a variable or method public is as shown below:

public Add() {}

Protected The final form of protection available in Java concerns the relationship between a class and its present and future sub-classes declared inside or outside a package. These subclasses are much closer to a particular class than to any other ‘outside’ classes, for the following reasons:

• Sub-classes usually ‘know’ more about the internal implementation of a super-class.

• Sub-classes are often written by a reliable programmer who knows the source code.

• Sub-classes are frequently modified as to enhance the representation of the data within a parent class.

To support a special level of visibility reserved for sub-classes that are required to be somewhat less restricted than private, Java offers an intermediate level of access between package and private called, protected.. The keyword protected implies that those methods and variables are accessible to all classes inside the package and only sub-classes outside the package.

A summary of types of protection The differences between various types of protection can be unclear particularly in the case of protected methods and variables. Tabledescribes different types of protection (access Specifiers).

                                                        Table Different types of protection.

Visibility

Public

Protected

Package (No modifier)

Private

From the same class

Y

Y

Y

Y

From any class in the same package

Y

Y

Y

N

From any class outside the package

Y

N

N

N

From a sub-class in the same package

Y

Y

Y

N

From a sub-class outside the same package

  

    Y

 

   Y

 

   N

  

     N

You’ll also like:

  1. Access Protection in Packages
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW