0% found this document useful (0 votes)
0 views

OOPJ-Notes-2-3

This document provides an overview of Java packages, including their definition, types, and usage in organizing classes and interfaces. It covers how to define a package, set the CLASSPATH, import packages, and the access protection mechanisms for class members. Additionally, it outlines visibility rules for classes and their members within and across packages.

Uploaded by

khaleel2791
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

OOPJ-Notes-2-3

This document provides an overview of Java packages, including their definition, types, and usage in organizing classes and interfaces. It covers how to define a package, set the CLASSPATH, import packages, and the access protection mechanisms for class members. Additionally, it outlines visibility rules for classes and their members within and across packages.

Uploaded by

khaleel2791
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

UNIT - II

CHAPTER -3
PACKAGES
Contents
 Introduction to Packages
 Defining a Package
 CLASSPATH
 Access Protection
 Importing Packages
 References
INTRODUCTION TO PACKAGES
Package: In Java, a package is a container of classes, interfaces and sub-packages.
Packages are used to avoid naming conflicts and to organize project-related classes, interfaces
and sub-packages into a bundle.
In Java, the packages have divided into two types:

 Build-in packages
 User-defined packages
The package is both naming and visibility control mechanism.

DEFINING A PACKAGE
The keyword package is used to define a package.

package myPackage;
class MyClass{
public static void main(String args[]){
System.out.println(“MyClass in new package”);
}
}

 The package statement defines a namespace in which classes are stored.


 If the package statement is omitted, the class names are put into the default package,
which has no name.
 Java uses file system directories to store packages.
 More than one file can include the same package statement.
 Hierarchy of packages can also be created. Ex: package java.util.Scanner;

FINDING PACKAGES AND CLASSPATH


How does Java run-time system know where to look for packages that you create>?
1. Java run-time system uses the current working directory as its starting point.
2. Specify a directory path or paths by setting the CLASSPATH environmental variable.
3. Use the –classpath option with java and javac to specify the path to your classes.
CLASSPATH environmental variable can be set through the system properties or through
eclipse
Set through the system properties

Set through the eclipse


Eclipse -> Preferences -> Build Path -> Classpath Variables
IMPORTING PACKAGES
 import keyword is used to import built-in and user-defined packages.
 The import statement must be after the package statement and before any other
statement.
Ex: import java.util.Date;
Import java.util.Scanner;
 The import statement imports only classes of the package, but not sub-packages and its
classes.
 If a class with same name exists in two different packages, to import such class use fully
qualified name.
Ex: import java.util.Date;
Import java.sql.Date;

ACCESS PROTECTION
Java addresses four categories of visibility for class members:
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

 Anything declared public can be accessed from anywhere.


 Anything declared private cannot be seen outside of its class.
 When a member does not have an explicit access specification, it is visible to subclasses
as well as to other classes in the same package. This is the default access.
 If we want to allow an element to be seen outside our current package, but only to
classes that subclass your class directly, then declare that element protected.
 A class has only two possible access levels: default and public.
 When a class is declared as public, it is accessible by any other code.
 If a class has default access, then it can only be accessed by other code within its same
package.
Class Member Access

PRIVATE NO MODIFIER PROTECTED PUBLIC

Same class Yes Yes Yes Yes

Same pkg subclass No Yes Yes Yes

Same pkg non-subclass No Yes Yes Yes

Diff pkg subclass No No Yes Yes

Diff pkg non-subclass No No No Yes

References:
1. Herbert Schildt, Java: The Complete Reference, 10thEdition, McGraw Hill
Education (India) Pvt. Ltd.
2. https://www.javatpoint.com/
3. http://www.btechsmartclass.com/java/java-tutorials.html

You might also like