Professional Documents
Culture Documents
Lecture6 Packages
Lecture6 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.net
Classes for networking
java.util
Contains classes such as vectors, hash tables, date etc.
java.applet
Classes for creating and implementing applets
Implicit in all programs: import java.lang.*; package statement(s) must appear first
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
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 }
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.*;
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(); } }
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
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;
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
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
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
// OK A has package scope // OK get() has package scope // Error! data is private
SOBUZ 29
Yes
Yes
Yes
No
Yes
Yes
No
No
Yes
No
No
No
SOBUZ
30
Example
Example: graphics.shapes
Example: graphics.otherstuff
Example: graphics.otherstuff