OOPS using JAVA CSE-E3
UNIT-IV
Topics to be covered:
Packages: Java API Packages, System Packages, Naming Conventions, Creating & Accessing a
Package, Adding Class to a Package, Hiding Classes, Sample Programs
Package in Java
A package is a namespace that organizes a set of related classes and interfaces
or
A package is a collection of built-in classes, interfaces, abstract classes and sub packages based
on their functionality
Purpose of package
Conceptually packages are similar to different folders/ directories and sub-directories on
your computers. The purpose of package concept is to provide common classes and interfaces for
any program separately. In other words if we want to develop any class or interface which is
common for most of the java programs than such common classes and interfaces must be place
in a package.
K.RaviKanth, Asst.Prof., Dept. of CSE, IIIT- RGUKT- Basara, T.S, IND Page 1
OOPS using JAVA CSE -E3
The Java Platform provides an enormous class library ( a set of packages) suitable for developing
various applications. This support in the library is known as “ Application Programming
Interfaces” [ API].
Ex:
A String object contains a state and behavior for character strings.
A File object allows a programmer to easily create, delete, inspect , compare or modify a
file over filesystem
A Socket object allows for the creation and use of network sockets
A GUI objects controls buttons and checkboxes and anything else related to Graphical
User Interfaces
A Package offers the following things:
Namespace
Organizational benefits of the related class files i.e., Classes , Interfaces, Enumerated
Types
Some level of security
K.RaviKanth, Asst.Prof., Dept. of CSE, IIIT- RGUKT- Basara, T.S, IND Page 2
OOPS using JAVA CSE -E3
Advantage of package
Package is used to categorize the classes and interfaces so that they can be easily
maintained
Application development time is less, because reuse the code
Application memory space is less (main memory)
Application execution time is less
Application performance is enhance (improve)
Redundancy (repetition) of code is minimized
Package provides access protection.
Package removes naming collision.
Type of package
Packages are classified into two [ 02] types :
1. Built-in Packages or Pre-defined Packages or Standard Packages[ Java API]
2. User Defined Packages
Built-in Package
Package which are already designed by the Sun Microsystems and supply as a part of
java software in the form of API support
These Built-in packages are present in compressed files called JAR files( a JAR file or
Java Archive is used for aggregating many files into one) “rt.jar” file present in JRE
A package declaration resides at the top of a Java Source file. All source files to be
placed in a package have a common package name
They are inturn classified into 2 catagories : Core Packages and Extension Packages
Some important Core packages are :
java.lang [ Basic Language Functionalities]
java.io [ File handling Operations]
java.util [ Utility classes and collection data structure classes]
java.math [Arbitary precision arithmetic]
java.awt [ Abstract window toolkit for native GUI components]
K.RaviKanth, Asst.Prof., Dept. of CSE, IIIT- RGUKT- Basara, T.S, IND Page 3
OOPS using JAVA CSE -E3
java.awt.event [ AWT related event handler mechanisms]
java.applet [ To create an Applet and communicate with applet context]
java.net [ Network Programming ]
java.sql [ Java Database Connectivity ( JDBC) to access databases]
java.swing [ Lightweight programming for platform-independent rich GUI components]
so on…
Some important extension packages are :
javax.swing
javax.sql
javax.servlet
javax.servlet.http
javax.servlet.jsp so on..
NOTE: To include a library ( Built-in) package we have to use import keyword
Example: import java.awt.*;
Note:
java.lang is a special package, as it is imported by default in all the classes
For any kind of java application implicitly java.lang package is available. The major
contribution of this package is it provides
Exceptional Handling classes
Multi-threading Support
String Handling
Wrapper classes
System Resources etc…
K.RaviKanth, Asst.Prof., Dept. of CSE, IIIT- RGUKT- Basara, T.S, IND Page 4
OOPS using JAVA CSE -E3
Rules to create user defined package
package statement should be the first statement of any package program.
Choose an appropriate class name or interface name and whose modifier must be public.
Any package program can contain only one public class or only one public interface but
it can contain any number of normal classes.
Package program should not contain any main class (that means it should not contain any
main())
modifier of constructor of the class which is present in the package must be public.
(This is not applicable in case of interface because interface have no constructor.)
The modifier of method of class or interface which is present in the package must be
public (This rule is optional in case of interface because interface methods by default
public)
Every package program should be save either with public class name or public Interface
name
Compile package programs
For compilation of package program first we save program with public className.java
and it compile using below syntax:
K.RaviKanth, Asst.Prof., Dept. of CSE, IIIT- RGUKT- Basara, T.S, IND Page 5
OOPS using JAVA CSE -E3
Syntax
javac -d . className.java
Syntax
javac -d path className.java
Explanations: In above syntax "-d" is a specific tool which is tell to java compiler create a
separate folder for the given package in given path. When we give specific path then it create a
new folder at that location and when we use . (dot) then it crate a folder at current working
directory.
Note: Any package program can be compile but can not be execute or run. These program can be
executed through user defined program which are importing package program.
User defined package
Our own created packages are known as User Defined Packages
We can group different classes, interfaces, abstract classes etc in a separate directory and
is known as a User-Defined Package
Java compiler has got an option called “-d” [ directory] which specifies where to place
the generated class files as a result of compilation
“package” is the keyword used in java language to specify the package name.
Package declaration should be the first statement of a java program
K.RaviKanth, Asst.Prof., Dept. of CSE, IIIT- RGUKT- Basara, T.S, IND Page 6
OOPS using JAVA CSE -E3
Now save the program as javac –d . A.java
Java compiler will create A.class file and will be placed in OOPS folder
Note : If you want to save in current working directory itself then use[ . ] Dot is a system
variable indicating present working directory
Now check whether the folder has got created or not
Now make use of “import” keyword to import the contents of a package in other java application
Save the file as B.java
Now Compile and Run the A and B .java files
Run B.java file
K.RaviKanth, Asst.Prof., Dept. of CSE, IIIT- RGUKT- Basara, T.S, IND Page 7
OOPS using JAVA CSE -E3
Note : If you want to create in any specific directory location then use path
Ex2:
Including an Interface and a Class within the same package
K.RaviKanth, Asst.Prof., Dept. of CSE, IIIT- RGUKT- Basara, T.S, IND Page 8
OOPS using JAVA CSE -E3
Ex 3: Creating a sub-package JAVA in side a main package OOPS
K.RaviKanth, Asst.Prof., Dept. of CSE, IIIT- RGUKT- Basara, T.S, IND Page 9
OOPS using JAVA CSE -E3
K.RaviKanth, Asst.Prof., Dept. of CSE, IIIT- RGUKT- Basara, T.S, IND Page 10
OOPS using JAVA CSE -E3
Example over Sub Package
Save the file as Test_SubPackages.java
Compile the file as javac –d .Test_SubPackages.java
Now you can see the sub-folders created
Java compiler will place Test_SubPackages.class file in A.B.C.D package and when you
wan to use this class in our application we should import it as follows:
import A.B.C.D.Test_SubPackages;
or
import A.B.C.D.*;
K.RaviKanth, Asst.Prof., Dept. of CSE, IIIT- RGUKT- Basara, T.S, IND Page 11
OOPS using JAVA CSE -E3
Difference between Inheritance and package
Inheritance concept always used to reuse the feature within the program between class to
class, interface to interface and interface to class but not accessing the feature across the
program.
Package concept is to reuse the feature both within the program and across the programs
between class to class, interface to interface and interface to class.
Difference between package keyword and import keyword
“package” keyword is always used for creating the undefined package and placing
common classes and interfaces.
import is a keyword which is used for referring or using the classes and interfaces of a
specific package.
Note: Java compiler will not allow us to import same contents present in multiple
packages. To overcome this we need to specify the package name and class name
explicitly as follows:
Packagename.classname
Ex: Pack1.Number or Pack2.Number
CASE STUDIES:
1. Design a class with named as Number which contains 2 methods add_Numbers and
sub_Numbers and place them in a package called “Calculator”. Now create another class
called Calculations and import Calculator package and print the addition and subtraction
of Numbers.
2. Design a package with name Pack1 which contains 2 classes 1 interface
3. Define the above interface methods in your class
4. Define one more package with the name of Pack2 and place 1 class and interface
5. Create a package content to understand the protected access modifier
K.RaviKanth, Asst.Prof., Dept. of CSE, IIIT- RGUKT- Basara, T.S, IND Page 12