Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

Packages in Java

Packages
 Java’s way to group or Collection of Classes and/ or Interfaces

 Usually done according to functionality

 Organized in a Hierarchical / Tree like Structure

2
Packages - Benefits
 Reusability: Package used by a program can be used by other
program [We can use classes from other programs without physically copying them into
program under development]

 Two Classes of two different Packages can have same name

3
Packages - Benefits
 Way to Hide Classes to prevent other Programs / Packages that
are meant for internal use only

 A way to separate Design and Coding.


 First, Design Classes and their Relationship
 Second, Write Java Code to implement Methods
 Possible to change the Implementation at any time

4
Types of Packages
Packages

Java API Packages User Defined Packages

lang util io awt net applet

5
Java API
Language support classes

Automatically imported
Java compilers uses these classes
java.lang Classes for
Primitive data types
Strings
Mathematical functions
Threads
Exceptions

6
Java API
Utility classes

Classes for
Vectors
java.util Hash Tables
Random Numbers
Date

Input/ Output support classes


java.io Classes for Input and Output Data

7
Java API
Classes for implementing GUI

Classes for
Buttons
java.awt Lists
Menus
Windows
Networking support classes

java.net Classes for communicating


Local computers and Internet Servers

Classes for creating and implementing applets


8
java.applet
Hierarchical View - java.awt package
java
awt

Color Class
Package
Graphics Class

Font Class

Image Class

9
Ways to access classes in a Package
 Using fully qualified class name
 This is done using package name containing class then appending the class name to it
using the dot operator.
java.util.Scanner
 In many situations we might want to use a class in a number places in the program or we
may like to use many of the classes contained in a package
 This can be achieved using as follows:
import packagename.classname; OR
 For all the Classes in the Package

10 import packagename.*;
Importing a Package
 import statement must appear at the top of the file before any class

declarations, import is a keyword.

import packagename.classname;

11
Importing a Package
import java.awt.Color;

import java.awt.*;

12
Naming Convention in a Package
double y = java.lang.Math.sqrt(x);

Package Name Class Name Function Name

13
Creating our own Packages
Name of the Package
Keyword

package newPackage; Semi Colon Symbol


public class FirstClass
{
Body of FirstClass Name of the Class
}

Note: .class files must be located in a directory where classes that will import the
package are located.
14
Rules - Creating our own Packages
1. Declare a Package using
package packageName;

2. Define a Class inside this package and declare it as public

3. Create a Sub-Directory under the Directory where main source


files are stored

Sub-Directory name = Package Name

15
Rules - Creating our own Packages

4. Store the file as classname.java where classname is the name


of the First Class

5. Compile classname.java

javac –d . filename.java

6. The .class file must be within Sub-Directory


16
Example 1 – Creation of Package
Subdirectory Name: cosy

package cosy; Package Name: cosy

public class PackageTest Source File: PackageTest.java


{
public void enjoy()
{
System.out.println("Enjoy...");
}
public void fun()
{
System.out.println("Fun...");
}
17 }
Example 1 – Sub-Directory

18
Example 1 – Create a Class under Package

19
Example 1 – Compile the Class of the Package

OR

20
Example 1 – Import the Created Package

21
Example 1 – Compile and Run the Java Code

22
Visibility Control
Access Modifier/ public protected friendly private private
Access Location (default) protected

Same Class Yes Yes Yes Yes Yes

Subclass in Same Package Yes Yes Yes Yes No

Other Classes in the Same Yes Yes Yes No No


Package

Subclass in Other Package Yes Yes No Yes No

Non-Subclass in Other Yes No No No No


Package
23
Adding classes to Packages
 Define the class and make it public
 Place the package statement
package cosy;
before the class definition as follows:
package cosy;
public class NewClass
{//body of class
}
 Store this as B.java file under the directory cosy.
 Compile NewClass.java file.
Hiding Classes
 When we import a package using asterisk

(*) all public classes are imported

 However we may prefer to “not import”

certain classes i.e. we may like to hide these

classes from accessing from outside of the

package.

 Such classes should be declared “not public”


Thank You

26

You might also like