• 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 » Packages » Access Protection in Packages
Next →
← Prev

Access Protection in Packages

By Dinesh Thakur

Access modifiers define the scope of the class and its members (data and methods). For example, private members are accessible within the same class members (methods). Java provides many levels of security that provides the visibility of members (variables and methods) within the classes, subclasses, and packages.

Packages are meant for encapsulating, it works as containers for classes and other subpackages. Class acts as containers for data and methods. There are four categories, provided by Java regarding the visibility of the class members between classes and packages:

  1. Subclasses in the same package
  2. Non-subclasses in the same package
  3. Subclasses in different packages
  4. Classes that are neither in the same package nor subclasses

The three main access modifiers private, public and protected provides a range of ways to access required by these categories.

ACCESS PROTECTION IN PACKAGESSimply remember, private cannot be seen outside of its class, public can be access from anywhere, and protected can be accessible in subclass only in the hierarchy.

A class can have only two access modifier, one is default and another is public. If the class has default access then it can only be accessed within the same package by any other code. But if the class has public access then it can be access from any where by any other code.

Example:
//PCKG1_ClassOne.java
package pckg1;
public class PCKG1_ClassOne{
  int a = 1;
  private int pri_a = 2;
  protected int pro_a = 3;
  public int pub_a = 4;
  public PCKG1_ClassOne() {
    System.out.println("base class constructor called");
    System.out.println("a = " + a);
    System.out.println("pri_a = " + pri_a);
    System.out.println("pro_a "+ pro_a);
    System.out.println("pub_a "+ pub_a);
  }
}

The above file PCKG1_ClassOne belongs to package pckg1, and contains data members with all access modifiers.

//PCKG1_ClassTwo.java
package pckg1;
class PCKG1_ClassTwo extends PCKG1_ClassOne {
  PCKG1_ClassTwo() {
    System.out.println("derived class constructor called");
    System.out.println("a = " + a);
    // accessible in same class only
    // System.out.println("pri_a = " + pri_a);
    System.out.println("pro_a "+ pro_a);
    System.out.println("pub_a =” + pub_a);
  }
}

The above file PCKG1_ClassTwo belongs to package pckg1, and extends PCKG1_ClassOne, which belongs to the same package.

//PCKG1_ClasslnSamePackage
package pckg1;
class PCKG1_ClassInSamePackage {
  PCKG1_ClassInSamePackage() {
   PCKG1_ClassOne co = new PCKG1_ClassOne();
   System.out.println("same package class constructor called");
   System.out.println("a = " + co.a);
   // accessible in same class only
   // System.out.println("pri_a = " + co.pri_a);
   System.out.println("pro_a "+ co.pro_a);
   System.out.println("pub_a = " + co.pub_a);
 }
}

The above file PCKG1_ClassInSamePackage belongs to package pckg1, and having an instance of PCKG1_ClassOne.

package PCKG1;
//Demo package PCKG1
public class DemoPackage1 {
   public static void main(String ar[]) {
    PCKG1_ClassOne ob1 = new PCKG1_ClassOne();
    PCKG1_ClassTwo ob2 = new PCKG1_ClassTwo();
    PCKG1_ClassInSamePackage ob3 = new PCKG1_ClassxnSamePackage();
   }
}

The above file DemoPackage1 belongs to package pckg1, and having an instance of all classes in pckg1.

package pckg2;
class PCKG2_ClassOne extends PCKG1.PCKG1_ClassOne {
   PCKG2_ClassOne() {
   System.out.println("derived class of other package constructor called");
   // accessible in same class or same package only
   // System.out.println("a = " + a);
   // accessible in same class only
   // System.out.println("pri_a = " + pri_a);
   System.out.println("pro_a = " + pro_a);
   System.out.println("pub_a = " + pub_a);
  }
}

The above file PCKG2_ClassOne belongs to package pckg2. extends PCKG I_ClassOne, which belongs to PCKG1, and it is trying to access data members of the class PCKGI_ClassOne.

//PCKG2_ ClassInOtherPackage
package pckg2;
class PCKG2_ClassInOtherPackage {
  PCKG2_ClassInOtherpackage() {
   PCKG1.PCKG1_ClassOne co = new PCKG1.PCKG1_ClassOne();
   System.out.println("other package constructor");
   // accessible in same class or same package only
   // System.out.println("a ,= " + co.a);
   // accessible in same class only
   // System.out.println("pri_a = " + co.pri_a);
   // accessible in same class, subclass of same or other package
   // System.out.println("pro_a = " + co.pro_a);
   System.out.println("pub_a = " + co.pub_a);
  }
}

The above file PCKG2_ClassInOtherPackage belongs to package pckg2, and having an instance of PCKG1_ClassOne of package pckg 1, trying to access its some data members.

// Demo package pckg2.
package pckg2;
public class DemoPackage2 {
  public static void main(String ar[]) {
    PCKG2_ClassOne obl = new PCKG2_ClassOne();
    PCKG2_ClassInOtherPackage ob2 = new PCKG2_ClassInOtherPackage();
  }
}

The above file DemoPackage2 belongs to package pckg2, and having an instance of all classes of pckg2 .

You’ll also like:

  1. What is Packages in Java ? with Example
  2. The four Ps of protection in java
  3. The Swing Packages
  4. What is Access Method? Explain Different type of Access Methods
  5. Access Time – What is disk access time?
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

SQL Tutorials

SQL Tutorials

  • SQL - Home
  • SQL - Select
  • SQL - Create
  • SQL - View
  • SQL - Sub Queries
  • SQL - Update
  • SQL - Delete
  • SQL - Order By
  • SQL - Select Distinct
  • SQL - Group By
  • SQL - Where Clause
  • SQL - Select Into
  • SQL - Insert Into
  • SQL - Sequence
  • SQL - Constraints
  • SQL - Alter
  • SQL - Date
  • SQL - Foreign Key
  • SQL - Like Operator
  • SQL - CHECK Constraint
  • SQL - Exists Operator
  • SQL - Drop Table
  • SQL - Alias Syntax
  • SQL - Primary Key
  • SQL - Not Null
  • SQL - Union Operator
  • SQL - Unique Constraint
  • SQL - Between Operator
  • SQL - Having Clause
  • SQL - Isnull() Function
  • SQL - IN Operator
  • SQL - Default Constraint
  • SQL - Minus Operator
  • SQL - Intersect Operator
  • SQL - Triggers
  • SQL - Cursors

Advanced SQL

  • SQL - Joins
  • SQL - Index
  • SQL - Self Join
  • SQL - Outer Join
  • SQL - Join Types
  • SQL - Cross Join
  • SQL - Left Outer Join
  • SQL - Right Join
  • SQL - Drop Index
  • SQL - Inner Join
  • SQL - Datediff() Function
  • SQL - NVL Function
  • SQL - Decode Function
  • SQL - Datepart() Function
  • SQL - Count Function
  • SQL - Getdate() Function
  • SQL - Cast() Function
  • SQL - Round() Function

Other Links

  • SQL - 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