0% found this document useful (0 votes)
127 views28 pages

OOPJ - Unit 5

This document discusses packages and interfaces in Java. It defines a package as a mechanism to encapsulate related classes, sub-packages, and interfaces. Packages are used to prevent naming conflicts, make classes easier to find and use, and provide access control. The document also discusses built-in packages like java.lang, interfaces as blueprints that classes must implement, and how interfaces allow for multiple inheritance in Java.

Uploaded by

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

OOPJ - Unit 5

This document discusses packages and interfaces in Java. It defines a package as a mechanism to encapsulate related classes, sub-packages, and interfaces. Packages are used to prevent naming conflicts, make classes easier to find and use, and provide access control. The document also discusses built-in packages like java.lang, interfaces as blueprints that classes must implement, and how interfaces allow for multiple inheritance in Java.

Uploaded by

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

Unit 5

Packages and Interfaces


Package
• Package in Java is a mechanism to encapsulate
a group of classes, sub packages and
interfaces.
• Package MyPackage;
Use of Package
• 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
• Making searching/locating and usage of classes,
interfaces, enumerations and annotations easier
• 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.
• Packages can be considered as data encapsulation
(or data-hiding).
Types of Package
Built-in Packages
• 1) java.lang: Contains language support classes(e.g
classed which defines primitive data types, math
operations). This package is automatically imported.
2) java.io: Contains classed for supporting input /
output operations.
3) java.util: Contains utility classes which implement
data structures like Linked List, Dictionary and support ;
for Date / Time operations.
4) java.applet: Contains classes for creating Applets.
5) java.awt: Contain classes for implementing the
components for graphical user interfaces (like button ,
;menus etc).
6) java.net: Contain classes for supporting networking
operations.
Example

javac -d directory javafilename


Access Package
1) import package.*

All Classes
& Interfaces
But not sub
packages
2. import package.classname;
3. import fully qualified name
Name Conflicts
Sub Package
Static import
• to access any static member of a class directly.
There is no need to qualify it by the class
name.
Interface
• Interfaces specify what a class must do and not how. It
is the blueprint of the class.
• An Interface is about capabilities like a Player may be
an interface and any class implementing Player must
be able to (or must implement) move(). So it specifies a
set of methods that the class has to implement.
• If a class implements an interface and does not provide
method bodies for all functions specified in the
interface, then class must be declared abstract.
• A Java library example is, Comparator Interface. If a
class implements this interface, then it can be used to
sort a collection.
• Abstraction, Multiple Inheritance, IS-A relationship.
Java 8 Interface (Improvement)
Example
• It is both permissible and common for classes
that implement interfaces to define additional
members of their own.
Accessing Implementations Through
Interface References

• c=Interface Reference Variable


• variable c is declared to be of the interface
type Callback, yet it was assigned an instance
of Client
• An interface reference variable only has
knowledge of the methods declared by its
interface declaration. Thus, c could not be
used to access nonIfaceMeth( ) since it is
defined by Client but not Callback.
Multiple Inheritance by Interface
Interface Inheritance
Instanceof operator
• The java instanceof operator is used to test
whether the object is an instance of the
specified type (class or subclass or interface).
• Comparison operator.
• If we apply the instanceof operator with any
variable that has null value, it returns false.
Abstract Class vs Interface

You might also like