Java program to demonstrate the accessibility between two different packages

Java » Java program to demonstrate the accessibility between two different packages

A package is a container that consists of related classes and interfaces. In other words, It is a namespace ( that provides a private region or container for grouping ).

 

We used two packages named “package1” having two classes “ and “package2” having only one class.

Case 1. If Class and its methods are public then it would accessible to other classes within the same package and can also be accessed by the class from a different package.

Case 2. If class and its methods are protected then it would still accessible to other classes within the same package but can’t be accessed by classes that are from a different package.

Case 3. With a private modifier, a class can neither be accessed by class in same package nor classes from a different package.

Case 4. If no modifier is specified, then the classes within the same package can only communicate but not with classes from a different package.

Program:

Package 1: Class demo

 

Package 1: Class Employee

 

Package 2: Class Employee

 

Output:


You must be logged in to take the quiz.

error: Content is protected !!