8/2/2019 Lecture6 Packages
1/34
Packages
8/2/2019 Lecture6 Packages
2/34
The main feature of OOP is its ability to support the reuseof code: Extending the classes
Extending interfaces The features in basic form limited to reusing the classeswithin a program.
What if we need to use classes from other programswithout physically copying them into the program under
development ? In Java, this is achieved by using what is known aspackages, a concept similar to class libraries in otherlanguages.
8/2/2019 Lecture6 Packages
3/34
8/2/2019 Lecture6 Packages
4/34
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, andexception
java.io Stream classes for I/O
java.awt (abstract windowing kit) Classes for implementing GUIwindows, 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
8/2/2019 Lecture6 Packages
5/34
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).
8/2/2019 Lecture6 Packages
6/34
Accessing Classes from Packages
Example I
There are two ways of accessing the classes stored inpackages: 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
8/2/2019 Lecture6 Packages
7/34
8/2/2019 Lecture6 Packages
8/34
4/17/2012 SOBUZ 8
Creating Your Own Package
1. Declare the package at the
beginning of a file using theform
packagepackagename;
2. Define the class that is to be put
in the package and declare it
public.
3. Create a subdirectory under the
directory where the main source
files are stored.
4. Store the listing as
classname.java in the
subdirectory created.
5. Compile the file. This creates
.class file in the subdirectory.
Example:
package firstPackage;
Public class FirstClass
{
//Body of the class
}
8/2/2019 Lecture6 Packages
9/34
4/17/2012 SOBUZ 9
Example1-Packagepackage p1;
public class ClassA{
public void displayA( )
{
System.out.println(Class A);
}
}
import p1.*;
Class testclass
{
public static void main(String str[])
{
ClassA obA=new ClassA();
obA.displayA();
}
}Source fileClassA.java
Subdirectory-p1
ClassA.Java and ClassA.class->p1
Source file-testclass.java
testclass.java and testclass.class->in
a directory of whichp1 is
subdirectory.
8/2/2019 Lecture6 Packages
10/34
Creating Packages
Java supports a keyword called package for creating user-
defined 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}
8/2/2019 Lecture6 Packages
11/34
Creating Sub Packages
Classes in one ore more source files can be part of thesame 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 andMath class in a subdirectory myPackage.
8/2/2019 Lecture6 Packages
12/34
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 packagecan be imported as follows:
import myPackage.*;
8/2/2019 Lecture6 Packages
13/34
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 bodypublic void display(){
System.out.println("Hello, I am ClassA");}
}class ClassB {// class body
}
8/2/2019 Lecture6 Packages
14/34
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();
}}
8/2/2019 Lecture6 Packages
15/34
Compiling and Running
When ClassX.java is compiled, the compilercompiles it and places .class file in currentdirectly. If .class of ClassA in subdirectorymyPackage is not found, it compiles ClassAalso.
Note: It does not include code of ClassA intoClassX
When the program ClassX is run, java loaderlooks for ClassA.class file in a package calledmyPackage and loads it.
8/2/2019 Lecture6 Packages
16/34
8/2/2019 Lecture6 Packages
17/34
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();
}
}
8/2/2019 Lecture6 Packages
18/34
Output
Hello, I am ClassA
Hello, I am ClassC
8/2/2019 Lecture6 Packages
19/34
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
8/2/2019 Lecture6 Packages
20/34
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
8/2/2019 Lecture6 Packages
21/34
8/2/2019 Lecture6 Packages
22/34
Accessing Packages
To define: add package statement to specifypackage 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;
8/2/2019 Lecture6 Packages
23/34
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 packageClass 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 ofthe package with the outside world
Importing a package does not automatically import subpackagesE.g. import java.awt.* does not import java.awt.font
bli / k / i
8/2/2019 Lecture6 Packages
24/34
4/17/2012 SOBUZ 24
public/package/private scope
Scope is concerned with the visibility of program
elements such as classes and membersClass members (methods or instance fields) can be
defined with public, package (default), private orprotected scope
A class has two levels of visibility:
-public scope means it is visible outside itscontaining package
- default scope means it is visible only inside thepackage. (package scope/ friendly scope)
8/2/2019 Lecture6 Packages
25/34
4/17/2012 SOBUZ 25
A class member with public scopemeans it is visible
anywhere its class is visible
A class member with private scopemeans 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.
public/package/private scope
8/2/2019 Lecture6 Packages
26/34
4/17/2012 SOBUZ 26
Example 1
package my_package;
class A // package scope{
// As public & private members}
public class B // public scope{
// Bs public and private members}
8/2/2019 Lecture6 Packages
27/34
4/17/2012 SOBUZ 27
package my_package;
class D{
// Ds public & private members
// Class D knows about classes A and B
private B b; // OKclass B has public scope
private A a; // OKclass A has package scope
}
Example-2
8/2/2019 Lecture6 Packages
28/34
4/17/2012 SOBUZ 28
package another_package;import my_package.*;
class C{
// Cs public & private members
// class C knows about class B
private B b; // OKclass B has public scope
}
Example-3
8/2/2019 Lecture6 Packages
29/34
4/17/2012 SOBUZ 29
Example 4
package my_package;
class A{
int get() { return data; } // package scopepublic A(int d) { data=d;} // public scopeprivate int data; // private scope
}
class B{
void f()
{ A a=new A(d); // OK A has package scopeint d=a.get(); // OKget() has package scopeint d1=a.data; // Error!data is private
}
}
Levels of Access Control
8/2/2019 Lecture6 Packages
30/34
4/17/2012 SOBUZ 30
Levels of Access Controlpublic protected friendly
(default)
private
sameclass
Yes Yes Yes Yes
Subclass in
the same
package
Yes Yes Yes No
Other classin the same
package
Yes Yes Yes No
Subclass in
other
packages
Yes Yes No No
Non-
subclass in
other
package
Yes No No No
8/2/2019 Lecture6 Packages
31/34
Example
8/2/2019 Lecture6 Packages
32/34
Example: graphics.shapes
8/2/2019 Lecture6 Packages
33/34
8/2/2019 Lecture6 Packages
34/34
Example: graphics Package