Docsity

Java Packages: Organizing and Reusing Code, Slides of Java Programming

Partial preview of the text

Download Java Packages: Organizing and Reusing Code and more Slides Java Programming in PDF only on Docsity!

Packages

Packages

 The main feature of OOP is its ability to support the reuse of code:  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.

Benefits of Packaging 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 don’t want other packages to access them.  Packages also provide a way for separating “design” from coding.

Java Foundation 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

Accessing Classes from Packages

 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

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 }

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.*;

Creating Packages

 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

 Let us store the code listing below in a file named “ClassC.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 “ClassY.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(); } }

Setting path and rumming the file

 set CLASSPATH=.;C:;

 C:\world\javac HelloWorld.java (world -> package name)

let's put the HelloWorld class along with its package (world) be under: C:\myclasses directory instead.The new location of our HelloWorld should be :  set CLASSPATH=.; C:\myclasses;C:\world>java world.HelloWorld

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

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