Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 34

Packages

The main feature of OOP is its ability to support the reuse of code:
Extending the classes Extending interfaces

The features in basic form limited to reusing the classes within a program. What if we need to use classes from other programs without physically copying them into the program under development ? In Java, this is achieved by using what is known as packages, a concept similar to class libraries in other languages.

Packages
Packages are Javas way of grouping a number of related classes together into a single unit. That means, packages act as containers for classes. The benefits of organising classes into packages are:
The classes contained in the packages of other programs/applications can be reused. In packages classes can be unique compared with classes in other packages. That two classes in two different packages can have the same name. If there is a naming clash, then classes can be accessed with their fully qualified name. Classes in packages can be hidden if we dont want other packages to access them. Packages also provide a way for separating design from coding.

Packages
Two types of packages: 1. Java API packages 2. User defined packages Java provides a large number of classes grouped into different packages based on their functionality. The six foundation Java packages are:
java.lang
Contains classes for primitive types, strings, math functions, threads, and exception

java.io
Stream classes for I/O

java.awt (abstract windowing kit)


Classes for implementing GUI windows, buttons, menus etc.

java.net
Classes for networking

java.util
Contains classes such as vectors, hash tables, date etc.

java.applet
Classes for creating and implementing applets

Using System Packages


The packages are organised in a hierarchical structure. For example, a package named java contains the package awt, which in turn contains various classes required for implementing GUI (graphical user interface).

Accessing Classes from Packages Example I


There are two ways of accessing the classes stored in packages:
Using fully qualified class name java.lang.Math.sqrt(x); Import package and use class name directly. import java.lang.Math; Math.sqrt(x);

Selected or all classes in packages can be imported:

Implicit in all programs: import java.lang.*; package statement(s) must appear first

Accessing Classes from Packages Example II


1. Fully Qualified class name: Example:java.awt.Color 2. import packagename.classname; Example: import java.awt.Color; or import packagename.*; Example: import java.awt.*;

Creating Your Own Package


1. Declare the package at the beginning of a file using the form package packagename; Define the class that is to be put in the package and declare it public. Create a subdirectory under the directory where the main source files are stored. Store the listing as classname.java in the subdirectory created. Compile the file. This creates .class file in the subdirectory.
4/17/2012

Example: package firstPackage; Public class FirstClass { //Body of the class }

2.

3.

4.

5.

SOBUZ

Example1-Package
package p1; public class ClassA { public void displayA( ) { System.out.println(Class A); } }
Source file ClassA.java Subdirectory-p1

import p1.*; Class testclass { public static void main(String str[]) { ClassA obA=new ClassA(); obA.displayA(); }

}
Source file-testclass.java

ClassA.Java and ClassA.class->p1

testclass.java and testclass.class->in a directory of which p1 is subdirectory.


SOBUZ 9

4/17/2012

Creating Packages
Java supports a keyword called package for creating userdefined packages. The package statement must be the first statement in a Java source file (except comments and white spaces) followed by one or more classes. The file is saved as ClassA.java and code is located in directory myPackage.
package myPackage; public class ClassA { // class body } class ClassB { // class body }

Creating Sub Packages


Classes in one ore more source files can be part of the same packages.

As packages in Java are organised hierarchically, sub-packages can be created as follows:


package myPackage.Math; package myPackage.secondPakage.thirdPackage;

Store thirdPackage in a subdirectory named myPackage\secondPackage. Store secondPackage and Math class in a subdirectory myPackage.

Accessing a Package
The general form of importing package is:
import package1[.package2][].classname Example:
import myPackage.ClassA; import myPackage.secondPackage.*;

All classes/packages from higher-level package can be imported as follows:


import myPackage.*;

Let us store the code listing below in a file named ClassA.java within subdirectory named myPackage within the current directory (say abc).
package myPackage; public class ClassA { // class body public void display() { System.out.println("Hello, I am ClassA"); } } class ClassB { // class body

Using a Package
Within the current directory (abc) store the following code in a file named ClassX.java
import myPackage.ClassA; public class ClassX { public static void main(String args[]) { ClassA objA = new ClassA(); objA.display(); } }

Compiling and Running


When ClassX.java is compiled, the compiler compiles it and places .class file in current directly. If .class of ClassA in subdirectory myPackage is not found, it compiles ClassA also. Note: It does not include code of ClassA into ClassX When the program ClassX is run, java loader looks for ClassA.class file in a package called myPackage and loads it.

Using a Package
Let us store the code listing below in a file named ClassA.java within subdirectory named secondPackage within the current directory (say abc).
package secondPackage; public class ClassC { // class body public void display() { System.out.println("Hello, I am ClassC"); } }

Within the current directory (abc) store the following code in a file named ClassX.java
import myPackage.ClassA; import secondPackage.ClassC; public class ClassY { public static void main(String args[]) { ClassA objA = new ClassA(); ClassC objC = new ClassC(); objA.display(); objC.display(); } }

Output
Hello, I am ClassA Hello, I am ClassC

Sub Packages
Packages can be divided into subpackages java.awt Classes for GUIs and graphics java.awt.font Classes and interface for fonts java.awt.geom Classes for 2-dimensional objects

Use fully qualified name: java.util.Scanner sc = new java.util.Scanner(System.in); Can use import to tell Java where to look for things defined outside your program import java.util.Scanner; Scanner sc = new Scanner (System.in); Using * imports all classes in package: import java.util.*; Scanner sc = new Scanner (System.in); Multiple import statements allowed java.lang automatically imported by every Java program

User Defined Packages


Why? Packages enable a programmer organize the code into smaller
logically related units A large program may consists of hundreds of classes (800 in one current project with NASA) Every class is part of some package If you do not specify a package a class becomes part of the default package

Access Classes defined within the same package can access one another
more easily (no need for imports, fully qualified names) Some classes, object fields only accessible to classes in same package

Accessing Packages
To define: add package statement to specify package containing the classes of a file
package mypackage; public class myClass { } // myClass is part of mypackage

Must be first non-comment statement in file Packages organized into subpackages using the notation
package mypackage.mysubpackage;

Class Access and Packages


Class access within a package
Classes within a package can refer to each other without full qualification If a class, or member within a class, is not declared public, it can only be accessed by other classes within the package

Class access across packages


A public class can be accessed from other packages Its name is fully qualified or it must be imported to achieve this The public classes of a package can be seen as the interface of the package with the outside world Importing a package does not automatically import subpackages E.g. import java.awt.* does not import java.awt.font

public/package/private scope
Scope is concerned with the visibility of program elements such as classes and members Class members (methods or instance fields) can be defined with public, package (default), private or protected scope A class has two levels of visibility: -public scope means it is visible outside its containing package

- default scope means it is visible only inside the package. (package scope/ friendly scope)
4/17/2012 SOBUZ 24

public/package/private scope
A class member with public scope means it is visible anywhere its class is visible

A class member with private scope means it is visible only within its encapsulating class
A class/class member with package scope means it is visible only inside its containing package A class member with protected scope means it is visible every where except the non-subclasses in other package.

4/17/2012

SOBUZ

25

Example 1
package my_package; class A // package scope { // As public & private members } public class B // public scope { // Bs public and private members }

4/17/2012

SOBUZ

26

Example-2
package my_package; class D { // Ds public & private members // Class D knows about classes A and B private B b; private A a; }
4/17/2012 SOBUZ 27

// OK class B has public scope // OK class A has package scope

Example-3
package another_package; import my_package.*;
class C { // Cs public & private members // class C knows about class B

private B b;
}
4/17/2012

// OK class B has public scope

SOBUZ

28

Example 4
package my_package; class A { int get() { return data; } public A(int d) { data=d;} private int data; } class B { void f() { A a=new A(d); int d=a.get(); int d1=a.data; } } 4/17/2012

// package scope // public scope // private scope

// OK A has package scope // OK get() has package scope // Error! data is private
SOBUZ 29

Levels of Access Control


public same class Subclass in the same package Yes Yes protected Yes Yes friendly (default) Yes Yes private Yes No

Other class in the same package


Subclass in other packages Nonsubclass in other package
4/17/2012

Yes

Yes

Yes

No

Yes

Yes

No

No

Yes

No

No

No

SOBUZ

30

Example

Example: graphics.shapes

Example: graphics.otherstuff

Example: graphics.otherstuff

Example: graphics Package

You might also like