Unit III
Inheritance, Interface and
Package
Mr. R. M. Patil
SY CO, JPR
2023-2024
3.4. Java Package
A java package is a group of similar types of classes,
interfaces and sub-packages.
Packages are used for:
1. Preventing naming conflicts. For example there can be two classes
with name Employee in two packages, [Link]
and [Link]
2. Making searching/locating and usage of classes, interfaces,
enumerations and annotations easier
3. Providing controlled access: protected and default have package
level access control. A protected member is accessible by classes in
the same package and its subclasses. A default member (without
any access specifier) is accessible by classes in the same package
only.
4. Packages can be considered as data encapsulation (or data-hiding).
3.4. Java Package
3.4. Java Package
3.4. Java Package – Types of Package
3.4. Java Package – Types of Package 1. Built in Package
The Java API is a library of prewritten classes, that are free
to use, included in the Java Development Environment.
The library is divided into packages and classes. Meaning
you can either import a single class (along with its methods
and attributes), or a whole package that contain all the
classes that belong to the specified package.
To use a class or a package from the library, you need to
use the import keyword:
import [Link]; // Import a single class
import [Link].*; // Import the whole package
3.4. Java Package – Types of Package 1. Built in Package
Applet.
3.4. Java Package – Types of Package 2. User Defined Package
The packages are defined by the user as per the
application requirement.
-How to create package in Java
1. First create a directory (Folder) within name of package.
2. Create a java file in newly created directory.
3. In this java file you must specify the package name with
the help of package keyword.
4. Save this file with same name of public class
Note: only one class in a program can declare as public.
5. Now you can use this package in your program.
3.4. Java Package – Types of Package 2. User Defined Package
[Link] [Link]
package pack; package pack;
public class A public class B
{ {
public void msg() public void display()
{ {
[Link]("Hello"); [Link](“Java");
} }
} }
Compile the program.
Remember that the program do not contain main()
method and we do not run the program. It just creates the
package.
3.4. Java Package – Types of Package 2. User Defined Package
Accessing Package, import statement
[Link]
[Link]
import pack.A;
import pack.*;
import pack.B;
class demo
class demo
{
{
public static void main()
{ Or public static void main()
{
A a1 = new A();
A a1 = new A();
B b1 = new B();
B b1 = new B();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
}
}
3.4. Static import
In Java, static import concept is introduced in 1.5 version.
With the help of static import, we can access the static
members of a class directly without class name or any
object.
For Example: we always use sqrt() method of Math class
by using Math class i.e. [Link](), but by using static
import we can access sqrt() method directly.
3.4. Static import
// Java Program to illustrate calling of predefined methods without static
import
class demo
{
public static void main(String[] args)
{
[Link]([Link](4));
[Link]([Link](2, 2));
[Link]([Link](6.3)); Output:
} 2.0
} 4.0
6.3
3.4. Static import
// Java Program to illustrate calling of predefined methods with static import
import static [Link].*;
class demo
{
public static void main(String[] args)
{
[Link](sqrt(4));
[Link](pow(2,2));
[Link](abs(6.3)); Output:
} 2.0
} 4.0
6.3
3.4. Adding interface to a Package
Interface is similar to a class, but it contains only
abstract methods.
By default the variables declared in an interface are
public, static and final.
Interface is a mechanism to achieve full abstraction.
An interface does not contain any constructor.
3.4. Adding interface to a Package
We have already seen how to add classes in package.
Adding interface in package is also a similar concept.
package Mypack;
interface base
{
public void msg();
}
Homework - 18
1. What is Package? W14,S15,W15,S17,W17 – 2M
2. List any four built-in packages from java API along with their
use. S17,W17,S15,W15 – 4M
3. How to create and access the package in java? Explain with
suitable example. W14,S15,W15,S17,W17 – 6M
4. Design a package containing a class which defines a method
to find area of rectangle. Import it in java application to
calculate area of a rectangle. – S16, 4M
5. How to add interface to a package. – W16, 2M