Professional Documents
Culture Documents
Together
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 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;
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
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
}
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
}
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;
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