2. Interfaces and Packages
Interfaces are java’s substitute for C++ feature of multiple
inheritance. It is the practice of allowing a class to have several
superclasses.
Java does not allow multiple inheritances.
Java classes ,however, can implement several interfaces.
An interface is purely an abstract class, but is different in that
all it contains is empty functions and perhaps static final
constants.
An interface can’t be instantiated. Its purpose is to specify a
set of requirements for a class to implement.
These requirements are regarded as a “contract” between the
implementing class and any client class that uses it.
A class can only inherit a single superclass but can implement
implement more than one interface.2
3. Interface declaration
[public]<interface><interfaceName>{ //…. }
Example:-interface definition
public interface Shape{
[public abstract] double area();
[public abstract]double volume();
String getName(); }
An interface is a description of a capability. It lists the methods that a class
must implement to carry out that capability.
An interface typically consists of declarations of related methods, although an
interface also may contain static and final fields that represent constants.
In any case, all interface members are public.
A class that implements an interface typically provides definitions for the
methods declared in the interface.
3
4. A class that implements an interface must provide an
implementation for all the method signatures in the interface.
An interface declaration introduces a new reference type
whose members are constants and abstract methods.
An interface is typically used in place of an abstract class when
there is no default implementation to inherit. i.e., no instance
variables and no default methods implementations.
A compile-time error occurs if the identifier naming an
interface appears as the name of any other class or interface in
the same package.
All interface methods are abstract regardless of whether the
term abstract occurs. an interface is abstract because an
interface can’t be instantiated as an object.
In Java it is possible for an interface to extend more than one
base interface.
4
5. Example:-Interface implementation
public interface Shape{
public double getArea();
public double getVolume();
public String getName();
}//end interface
public class Point extends Object implements Shape{
private int xCoord, yCoord;
public Point(){ }
public Point(int x, int y){ xCoord=x; yCoord=y; }
public int getX(){ return xCoord; }
public int getY(){ return yCoord; }
public String toString(){ return “point = (“+getX()+”,”+ getY()+”)”;
}
public double getArea(){ return 0.0; }
public double getVolume(){ return 0.0;}
public double getName(){ return “Point”;}; }5
Overridden
Methods
6. Interface inheritance
In Java a class can be derived only from one base class. That is the following
declarations are not allowed.
class C extends A,B{// …. body }// wrong !!
But an interface can extend multiple interfaces and a class can implement
multiple interfaces. Refer the following interface inheritance diagram.
6
Soakable
Scrubabl
e
BubbleBathab
le
7. Based on the diagram given in the previous slide, you can
declare the following interfaces.
public interface Washable{
void wash();
}
public interface Soakable extends Washable{
void soak();
}
public interface Scrubable extends Washable{
void Scrubable();
}
public interface BubbleBathable extends
Soakable,Scrubable{
void takeBubbleBath();
}
7
8. A class can implement several interfaces.
public class CoffeCup implements BubbleBathable,
Breakable{
void wash(){ // }
void soak(){ // }
void scrub(){ // }
void takeBubbleBath(){ // }
void breakIt(){ // }
}
Any class that implements a sub interface must override (implement )
all the abstract methods found in the sub-interface or in the ancestors
of the sub-interface
8
9. PACKAGES
Packages are containers for classes that are used to keep the class name
space compartmentalized.
A package contains a set of related classes.
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 you omit the package statement, the
class names are put into the default package.
Syntax:
package packageName;
A package name consists of one or more identifiers separated by periods.
there is a special package, called the default package, which has no name.
If you did not include any package statement at the top of your source
file, its classes are placed in the default package.
9
10. If you want to use a class from a package, you can refer to it by its full
name(package name plus class name). For example, java.io.*; refers
to all the class in the java.io package:
The ‘import’ directive lets you refer to a class of a package by its class
name, without the.
You cannot rename a package prefix without renaming the directory in
which the classes are stored.
A package name corresponds to the directory name where the file is
located.
Remember that case is significant, and the directory name must
match the package name exactly.
By default the java.lang.*; package will be imported.
10
Contd...
11. The syntax for a multileveled package statement is:
package pkg1[.pkg2[.pkg3]];
A package is located in a subdirectory that matches the
name. The parts of the name between periods represent
successively nested directories.
For example, the package amu.univ.it.project.bigjava would
be placed in a subdirectory amu/univ/it/project/bigjava.
In a large project, it is inevitable that two people will come up with
the same name for the same concept. It is important to avoid such
name clashes. To alleviate such problem use a domain name in
reverse to construct unambiguous package names.
Classes and packages are both means of encapsulating and
containing the name space and scope of variables and methods.
Packages act as containers for classes and other subordinate
packages.
11
12. Creating and using packages
If you want to make a package of a group of classes, called say myPack,
do the following:
1. Create a folder called myPack and place the .java files into the folder.
2. Create Java file(s) and save the file(s) within myPack folder
Note that each class within the package begins with package
myPack;
3. Compile the java file(s).
4. Write the main class which imports the package and save it in the
myPack folder.
The main class has to import the package like import myPack.*;
5. Compile the main class
6. Run the main class12
13. Access Modifiers
Where
Members are found
in?
Access modifiers type
Private Default protecte
d
public
Same class yes Yes Yes yes
Same package
subclass
No yes Yes Yes
Same package
non-subclass
No Yes yes Yes
Different package
subclass
No No Yes Yes
Different package
non-subclass
No No no Yes
13