0% found this document useful (0 votes)
10 views16 pages

Chapter 3.4

The document discusses Java packages, which are groups of similar classes, interfaces, and sub-packages that help prevent naming conflicts, simplify class usage, and provide access control. It outlines the types of packages, including built-in and user-defined packages, and explains how to create and access them in Java. Additionally, it introduces the concept of static import and how to add interfaces to packages.

Uploaded by

sanikakabade07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views16 pages

Chapter 3.4

The document discusses Java packages, which are groups of similar classes, interfaces, and sub-packages that help prevent naming conflicts, simplify class usage, and provide access control. It outlines the types of packages, including built-in and user-defined packages, and explains how to create and access them in Java. Additionally, it introduces the concept of static import and how to add interfaces to packages.

Uploaded by

sanikakabade07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

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, college.staff.cse.Employee
and college.staff.ee.Employee
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 package.name.Class; // Import a single class


import package.name.*; // 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
A.java B.java

package pack; package pack;


public class A public class B
{ {
public void msg() public void display()
{ {
System.out.println("Hello"); System.out.println(“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
demo.java
demo.java
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();
a1.msg();
a1.msg();
b1.display();
b1.display();
}
}
}
}
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. Math.sqrt(), 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)
{
System.out.println(Math.sqrt(4));
System.out.println(Math.pow(2, 2));
System.out.println(Math.abs(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 java.lang.Math.*;


class demo
{
public static void main(String[] args)
{
System.out.println(sqrt(4));
System.out.println(pow(2,2));
System.out.println(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

You might also like