You are on page 1of 24

Packages: Putting Classes

Together

Md. Rashid Al Asif


Assistant Professor, Dept. of Computer Science and
Engineering
University of Barishal, Barishal-8254
Contents
Introduction
Java API packages
Using system packages
Naming conventions
Creating package
Accessing package
Using a package
Misc
Summary

2
Introduction
Packages are those class which contains
methods without the main method in them
mainly used to reuse the classes which are
already created/used in other program
We can define many number of classes in the
same package
Two types
⚫ Built-in
⚫ User defined

3
Advantages
Mainly used to categorize the classes and
interfaces so that they can be easily maintained
Java package provides access protection
Java package removes naming collision

4
Frequently used API packages

5
Built-in Packages

6
Using System Package

import packagename.classname; or import packagename.*;

import java.awt.Color;
7
Naming Convention

8
Creating Package (1)

Compiling:
javac -d . Simple.java

• The -d switch specifies the destination where to put the generated class file
• If you want to keep the package within the same directory, you can use . (dot)

9
Creating Package (2)
Declare the package at the beginning of a file
using the form
package packagename;

Define the class that is to be put it the package


and declare it public
Create a sub directory under the directory where
the main source files are stored
Store the listing as the classname.java file in the
sub directory created
Compile the file. This creates .class file in the sub
directory
10
Accessing a Package
import package1 [.package2] [.package3]
.classname;

import firstPackage.secondPackage.MyClass;

import packagename.*;

11
Using a Package (1)

12
Using a Package (2)

13
Protection and Packages
All classes (or interfaces) accessible to all others
in the same package.
Class declared public in one package is
accessible within another. Non-public class is not
Members of a class are accessible from a
difference class, as long as they are not private
protected members of a class in a package are
accessible to subclasses in a different class

14
Visibility - Revisited
Public keyword applied to a class, makes it
available/visible everywhere. Applied to a method
or variable, completely visible.
Private fields or methods for a class only visible
within that class. Private members are not visible
within subclasses, and are not inherited.
Protected members of a class are visible within
the class, subclasses and also within all classes
that are in the same package as that class.

15
Visibility Modifiers

Accessible to: public protected Package private


(default)

Same Class Yes Yes Yes Yes

Class in package Yes Yes Yes No

Subclass in Yes Yes No No


different package

Non-subclass Yes No No No
different package

16
Adding a Class to a Package
Consider an existing package that contains a
class called “Teacher”:
package pack1;
public class Teacher
{
// class body
}

This class is stored in “Teacher.java” file within a


directory called “pack1”.
How do we a new public class called “Student” to
this package.

17
Adding a Class to a Package
Define the public class “Student” and place the package
statement before the class definition as follows:
package pack1;
package pack1;
public class Student
{ class Teacher
// class body
class Student
}

Store this in “Student.java” file under the directory “pack1”.

When the “Student.java” file is compiled, the class file will


be created and stored in the directory “pack1”. Now, the
package “pack1” will contain both the classes “Teacher”
and “Student”.

18
18
Packages and Name Clashing
When packages are developed by different organizations,
it is possible that multiple packages will have classes with
the same name, leading to name classing.
package pack1; package pack2;

class Teacher class Student

class Student class Courses

We can import and use these packages like:


⚫ import pack1.*;
⚫ import pack2.*;
⚫ Student student1; // Generates compilation error

19
19
Handling Name Clashing
In Java, name classing is resolved by accessing
classes with the same name in multiple packages
by their fully qualified name.

Example:
import pack1.*;
import pack2.*;
pack1.Student student1; // OK
pack2.Student student2; // OK
Teacher teacher1; // No problem
Courses course1; // No problem

20
20
Extending a Class from Package
A new class called “Professor” can be created by
extending the “Teacher” class defined the
package “pack1” as follows:

import pack1.Teacher;
public class Professor extends Teacher
{
// body of Professor class
// It is able to inherit public and protected members,
// but not private or default members of Teacher class.
}

21
21
Summary
Packages allow grouping of related classes into a
single united.
Packages are organised in hierarchical structure.
Packages handle name clashing issues.
Packages can be accessed or inherited without
actual copy of code to each program.

22
22
Self-Study
Static Import
More
⚫ https://www.javatpoint.com/package
⚫ https://www.geeksforgeeks.org/packages-in-java/
⚫ https://www.tutorialspoint.com/java/java_packages.htm

23
References
If not otherwise indicated, “google image”
Internet Resources
⚫ https://www.javatpoint.com/java-tutorial
⚫ https://www.tutorialspoint.com/java/index.htm
⚫ https://www.geeksforgeeks.org/
Books
⚫ Java: The Complete Reference
⚫ Programming with Java: A Primer

24

You might also like