0% found this document useful (0 votes)
15 views51 pages

Chapter 9

The document discusses packages and interfaces in Java. It explains what packages are and how they are used to organize classes into groups. It also covers how to define and implement interfaces in Java, including why interfaces are used and how classes can access interface implementations through interface references.

Uploaded by

gehaf92842
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)
15 views51 pages

Chapter 9

The document discusses packages and interfaces in Java. It explains what packages are and how they are used to organize classes into groups. It also covers how to define and implement interfaces in Java, including why interfaces are used and how classes can access interface implementations through interface references.

Uploaded by

gehaf92842
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/ 51

CHAPTER 9

PACKAGES AND INTERFACES


PACKAGES
 Till now all java classes were stored in same name
space.
 As the applications grows it becomes difficult to keep all
the files in same directory, also we need to ensure that
the name we choose should be unique. To address this
problem java introduces a mechanism to manage all the
classes this is called packages
 packages are containers for classes that are used to
keep the class name space compartmentalized.
 It is both a naming and a visibility control mechanism
PACKAGES
 To create a package simply include a package command as
the first statement in a Java source file.
 Any classes declared within that file will belong to the
specified package .
 The package statement defines a name space in which
classes are stored.
 If we omit the package statement, the class names are put
into the default package, which has no name
 General form of the package statement:
package pkgname;

 Eg: package mypackage;


Package and CLASSPATH
 How does the Java run-time system know where to look for
packages that you create?
 First, by default, the Java run-time system uses the current
working directory as its starting point. Thus, if your package is in a
subdirectory of the current directory, it will be found.
 Second, you can specify a directory path or paths by setting the
CLASSPATH environmental variable.
 Third, you can use the –classpath option with java and javac to
specify the path to your classes
 For eg consider package MyPackage
In order for a program to find MyPackage, one of three things
must be true. Either the program can be executed from a
directory immediately above MyPackage, or the CLASSPATH must
be set to include the path to MyPackage, or the –classpath option
must specify the path to MyPackage when the program is run via
java.
Package Example
Package Example
Java uses file system directories to store packages.
The .class files for any classes you declare to be part of
MyPack must be stored in a directory called MyPack/ is
significant, and the directory name must match the package
name exactly.
More than one file can include the same package statement.
The package statement simply specifies to which package the
classes defined in a file belong. It does not exclude other
classes in other files from being part of that same package.
 To create package hierarchy separate each
package name from the one above it by use of
a period.
package pkg1[.pkg2[.pkg3]];
package java.awt.image;

 A package hierarchy must be reflected in the


file system of your Java development system.
For example, a package declared as
package
oop.amp.btech.packages
needs to be stored in following directory

oop\amp\btech\packages
Package and CLASSPATH
 How does the Java run-time system know where to look for
packages that you create?
 First, by default, the Java run-time system uses the current
working directory as its starting point. Thus, if your package is in a
subdirectory of the current directory, it will be found.
Package and CLASSPATH
Package and CLASSPATH
Package and CLASSPATH
 How does the Java run-time system know where to look for
packages that you create?
Second, you can specify a directory path or paths by setting the
CLASSPATH environmental variable.

Package and CLASSPATH
 How does the Java run-time system know where to look for
packages that you create?
Third, you can use the –classpath option with java and javac to
specify the path to your classes.
Access Protection
Access Protection
While Java's access control mechanism may seem complicated,
we can simplify it as follows.
 Anything declared public can be accessed from anywhere.
Anything declared private cannot be seen outside of its class.
When a member does not have an explicit access specification,
it is visible to subclasses as well as to other classes in the same
package. This is the default access.
 If you want to allow an element to be seen outside your current
package, but only to classes that subclass your class directly,
then declare that element protected.
Reference:
https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
Access Protection
Access Protection
Access Protection
Access Protection
Access Protection
Importing Packages
In java all of the standard classes are stored in some named
package.
If we need use some classes from different package we should
import that package using import statement
Once imported, a class can be referred to directly, using only its
name.
To specify either an explicit classname or a star (*), which
indicates that the Java compiler should import the entire
package.
import pkg1[.pkg2].(classname|*);
import java.util.Date;
import java.io.*;
Importing Packages
Importing Packages
TYPES OF INHERITANCE
TYPES OF INHERITANCE
Multiple inheritance is not supported in java through class.
Interfaces
Using interface, we can fully abstract a class' interface from its
implementation.
Using interface, we can specify what a class must do, but not
how it does it.
Interfaces are syntactically similar to classes, but they lack
instance variables, and their methods are declared without any
body.
Once it is defined, any number of classes can implement an
interface.
One class can implement any number of interfaces.
To implement an interface, a class must create the complete set
of methods defined by the interface.
Each class is free to determine the details of its own
implementation
Why interface?
 Interfaces are designed to support dynamic method
resolution at run time.
 Normally, in order for a method to be called from one
class to another, both classes need to be present at
compile time so the Java compiler can check to ensure
that the method signatures are compatible. This
requirement by itself makes for a static and non
extensible classing environment.
Defining interface

access can be public or default


The methods which are declared have no bodies(abstract). They end
with a semicolon after the parameter list.
Each class that includes an interface must implement all of the
methods.
Variables can be declared inside of interface declarations. They are
implicitly final and static, meaning they cannot be changed by the
implementing class.
Variables must also be initialized with a constant value. All methods
and variables are implicitly public if the interface, itself, is declared
as public
interface Example
interface Callback {
void callback(int param);
}

Once an interface has been defined, one or more classes can


implement that interface
Implementing interface
To implement an interface, include the implements clause in a
class definition, and then create the methods defined by the
interface

 If a class implements more than one interface, the interfaces


are separated with a comma
 If a class implements two interfaces that declare the same
method, then the same method will be used by clients of either
interface. Methods implementing interface has to be declared
as public
 The type signature of the implementing method must match
exactly the type signature specified in the interface definition
interface left{
public void m1();
}
interface right{
public void m1();
}
class Test implements left,right
{
public void m1(){
System.out.println("Hi");
}
public static void main(String args[]){
Test t = new Test();
t.m1();
}
}
Output ?
interface InterfaceX
{ public int geek();
}
interface InterfaceY
{ public String geek();
}
public class Testing implements InterfaceX, InterfaceY
{
public String geek()
{ return "hello";
}
public static void main(String args[]){
Testing t = new Testing();
System.out.println(t.geek());
}
}
Output ?
Implementing interface

 It is both permissible and common for classes that implement


interfaces to define additional members of their own
Accessing implementation through interface references

 It is possible to declare variables as object references that use an


interface rather than a class type.
 Any instance of any class that implements the declared interface
can be stored in such a variable. When you call a method through
one of these references, the correct version will be called based
on the actual instance of the interface being referred to. This is
one of the key features of interfaces.

 c.callback(42) will call implementation in Client


class
Accessing implementation through interface references
 The method to be executed is looked up dynamically at run
time, allowing classes to be created later than the code which
calls methods on them.
 The calling code can dispatch through an interface without
having to know anything about the "callee." This process is
similar to using a superclass reference
 access a subclass object, as described in Dynamic Method
Dispatch
Partial Implementations
 If a class includes an interface but does not fully implement the
methods defined by that interface, then that class must be
declared as abstract.
abstract class Incomplete implements Callback {
int a, b;
void show() {
System.out.println(a + " " + b);
}
// ...
}
 Here, the class Incomplete does not implement callback() and
must be declared as abstract.
 Any class that inherits Incomplete must implement callback() or
be declared abstract itself.
Nested Interfaces
 An interface can be declared a member of a class or another
interface.
 A nested interface can be declared as public, private, or
protected. This differs from a top-level interface, which must
either be declared as public or use the default access level
 When a nested interface is used outside of its enclosing scope, it
must be qualified by the name of the class or interface of which it
is a member.
Nested Interfaces
Applying Interfaces
 The Stack class that implemented a simple fixed-size stack
 It can be growable.
 It was implemented with array. It can be implemented with linked
list, vector, trees etc.
 No matter how the stack is implemented, the interface to the
stack should remains the same like push and pop functions
 The interface to a stack is separate from its implementation, it is
easy to define a stack interface, leaving it to each implementation
to define the specifics.
Applying Interfaces
Applying Interfaces
Applying Interfaces
Applying Interfaces
Variables in Interfaces
 You can use interfaces to import shared constants into multiple
classes by simply declaring an interface that contains variables
which are initialized to the desired values.
 If an interface contains no methods, then any class that includes
such an interface doesn't actually implement anything. It is as if
that class were importing the constant variables into the class
name space as final variables.
Variables in Interfaces
Variables in Interfaces
Variables in Interfaces

Output:
INTERFACES CAN BE
EXTENDED
One interface can inherit another by use of the keyword
extends. The syntax is the same as for inheriting
classes.
When a class implements an interface that inherits
another interface, it must provide implementations for
all methods defined within the interface inheritance
chain
// One interface an extend another.
interface A {
void meth1(); public void meth3() {
void meth2(); System.out.println("Implement
} meth3().");
// B now includes meth1() and meth2()- }
// it adds meth3(). }
interface B extends A {
void meth3(); class IFExtend {
} public static void main(String arg[]) {
MyClass ob = new MyClass();
// This class must implement all of A and B
class MyClass implements B { ob.meth1();
public void meth1() { ob.meth2();
System.out.println("Implement ob.meth3();
meth1()."); }
} }
public void meth2() {
System.out.println("Implement
meth2().");
}

You might also like