8 Package
8 Package
Packages
Provides a mechanism for grouping a variety of
classes and / or interfaces together.
Grouping is based on functionality.
Benefits:
The classes contained in the packages of other
programs can be reused.
In packages, classes can be unique compared with
classes in other packages.
Packages provides a way to hide classes.
Packages
Two types of packages: 1. Java API packages 2. User defined
packages
Java API Packages:
A large number of classes grouped into different packages
based on functionality. Examples:
1. java.lang
2. java.util
3. java.io
4. java.awt
5.java.net
6.java. applet etc.
Package
Java
Package containing awt package
awt
Graphics
Image
Accessing Classes in a Package
1. Fully Qualified class name:
Example:java.awt.Color
2. import packagename.classname;
Example: import java.awt.Color;
or
import packagename.*;
Example: import java.awt.*;
Import statement must appear at the top of the file,
before any class declaration.
Creating Your Own Package
1. Declare the package at the
beginning of a file using the form
Example:
package packagename; package firstPackage;
2. Define the class that is to be put in
the package and declare it public.
3. Create a subdirectory under the Public class FirstClass
directory where the main source
files are stored. {
4. Store the listing as classname.java
in the subdirectory created. //Body of the class
5. Compile the file. This creates .class }
file in the subdirectory.
Example1-Package
package p1; import p1.*;
public class ClassA
{ Class testclass
public void displayA( ) {
{ public static void main(String str[])
System.out.println(“Class A”); {
} ClassA obA=new ClassA();
} obA.displayA();
}
Source file – ClassA.java
}
Subdirectory-p1 Source file-testclass.java
ClassA.Java and ClassA.class->p1 testclass.java and testclass.class->in
a directory of which p1 is
subdirectory.
Sequence of the program must be package then import then class.
12/18/24
Creating Packages
Compile-javac c:\p1\ClassA.java
Compile-javac PackageTest1.java
Copy –PackageTest1.class -> c:\
Class file in –c:\p1\ClassA.class
Execute-java PackageTest1
Adding a Class to a Package
Every java source file can contain only class declared as
public.
The name of the source file should be same as the name of the
public class with .java extension.
12/18/24 18
public/package/private scope
Scope is concerned with the visibility of program
elements such as classes and members
Class members (methods or instance fields) can be
defined with public, package (default), private or
protected scope
A class has two levels of visibility:
-public scope means it is visible outside its containing
package
- default scope means it is visible only inside the
package. (package scope/ friendly scope)
public/package/private scope
A class member with public scope means it is visible
anywhere its class is visible
we will create two packages and the classes in the packages will
be having the default access modifiers and we will try to access a
class from one package from a class of second package.
Program in which you will
declare a default method in
the base class of a different
package and use that method
in the child class of a different
package.
12/18/24 23
package p1;
public class class_in_package
{
void print1()
{
System.out.println("message in package class");
}
public void show()
{
print1();
}
}
12/18/24 24
import p1. class_in_package;
class lakshay extends class_in_package
{
void display()
{
class_in_package mca = new class_in_package ();
mca.show();
}
public static void main (String args[])
{
lakshay l_128 = new lakshay();
l_128.display();
}
}
12/18/24 25
Private access modifier