4Interfaces
4Interfaces
Multiple Inheritance
• Java does not support multiple inheritance.
• Classes in Java cannot have more than one superclass.
• Java provides an alternate approach known as interfaces
to support the concept of multiple inheritance.
• Although a Java class cannot be a subclass of more than
one superclass, it can implement more than one interface.
Interfaces
• Interface is basically a kind of class.
• Interface contain methods and variables but with a major
difference.
• The difference is that interfaces define only abstract
methods and final fields.
• Interfaces do not specify any code to implement these
methods and data fields contain only constants.
• It is the responsibility of the class that implements an
interface to define the code for implementation of these
methods.
Defining interfaces
• Syntax:
interface InterfaceName
{
variables declaration;
methods declaration;
}
Here interface is the keyword and InterfaceName is any valid Java
identifier name.
• Variables are declared as:
static final type VariableName=value;
Variables are declared as constants.
• Methods declaration will contain only a list of methods
without any body statements.
Return-type methodName(parameter-list);
Defining interfaces
• Eg:
interface Item
{
static final int code=1001;
static final String name=“asd”;
void display();
}
• Eg:
interface Area
{
final static float pi=3.142F;
float compute(float x, float y);
void show();
}
Defining interfaces
• Like classes, interface can also be extended.
• An interface can be subinterfaced from other interfaces.
• The new subinterface will inherit all the members of the
superinterface in the manner similar to subclasses.
• This is achieved using the keyword extends:
interface name2 extends name1
{
body of name2;
}
Extending interfaces
• For example we can put all the constants in one interface and the
methods in other.
• This will enable us to use the constants in classess where the methods
are not required.
• Eg:
interface ItemConstants
{
int code=1001;
String name=“Abc”;
}
interface Item extends ItemConstants
{
void display( );
}
• Note that the variables name and code are declared like simple
variables. It is allowed because all the variables in an interface are
treated as constants although the keywords final and static are not
present.
Extending interfaces
• We can also combine several interfaces together into a single interface.
• interface ItemConstants
{
int code=1001;
String name=“Abc”;
}
interface ItemMethods extends ItemConstants
{
void display( );
}
interface Item extends ItemConstants, ItemMethods
{
-------------
}
• While interfaces are allowed to extend to other interfaces, subinterfaces cannot
define the methods declared in the superinterfaces.
• It is the responsibility of any class that implements the derived interface to
define all the methods.
• An interface cannot extend classes.
Extending interfaces
• class classname implements Interfacename
{
body of classname
}
Here the class classname implements the interface Interfacename.
• class classname extends superclass
implements Interface1, Interface2,…
{
body of classname
}
A class can extend another class while implementing interfaces.
• When a class implements more than one interface, they are
separated by a comma.
Implementing interfaces
interface Area
{
final static float pi=3.14F;
float compute(float x, float y);
}
class Rectangle implements Area
{ public float compute(float x, float y)
{ return(x*y);
}
}
class Circle implements Area
{ public float compute(float x, float y)
{ return(pi*x*x);
}
}
class InterfaceTest
{ public static void main(String args[]){
Rectangle rect=new Rectangle();
Circle cir=new Circle();
System.out.println("Area of Rectangle ="+rect.compute(10,20));
System.out.println("Area of Circle ="+cir.compute(10,0));
}}
Implementing interfaces
• Any number of dissimilar classes can implement an
interface.
• If a class that implements an interface does not
implement all the methods of the interface, then the class
becomes an abstract class and cannot be instantiated.
Implementing interfaces
Various forms of interface
implementation
• Interfaces can be used to declare a set of constants that can be used in different classes.
• Since such interfaces do not contain methods, there is no need to worry about implementing
any methods.
• The constant values will be available to any class that implements the interface.
• The values cam be used in any method, as part of any variable declaration, or anywhere
where we can use a final value.
• Eg:
interface A
{ int m=10;
int n=50;
}
class B implements A
{
int x=m;
void methodB(int size)
{
…
if(size<n)
…
}
}