Chapter 10: Interfaces: Multiple Inheritance
10.1 Introduction
Java provides an alternative approach to handle multiple inheritance known as
“Interfaces”
Although a Java class cannot be a subclass of more than one superclass, it can
implement more than one interface, thereby enabling us to create classes that build upon
other classes without the problem created by multiple inheritance.
10.2 Defining Interfaces
An interface is basically a kind of class.
Interfaces contain methods and variables.
Interfaces define only abstract methods and final fields.
Interfaces do not specify any code to implement these methods and data fields contain
only constants.
The general form of an interface definition is:
interface InterfaceName
{
Variables declaration;
Methods declaration;
}
o interface is the key word and InterfaceName is a valid variable.
Variables are declared as follows:
static final type VariableName = Value;
o All variables are declared as constants.
Method declaration is as follows:
return-type methodName (parameter-list);
Eg:
interface Item
{
static final int code = 1001;
static final String name = “Fan”;
void display();
}
10.3 Extending Interfaces
Interfaces can be extended
An interface can be subinterfaced from other interfaces.
The new subinterface will inherit all the members of the superinterface.
Can be achieved using the keyword “extends”
The general form is:
interface name2 extends name1
{
Body of name2;
}
While interfaces are allowed to extend to other interfaces, subinterfaces cannot defines
the methods declared in the superinterfaces.
When an interface extends 2 or more interfaces, they are separated by commas.
An interface cannot extend classes
10.4 Implementing Interfaces
Interfaces are used as “superclasses” whose properties are inherited by classes.
10.1 Introduction
Java provides an alternative approach to handle multiple inheritance known as
“Interfaces”
Although a Java class cannot be a subclass of more than one superclass, it can
implement more than one interface, thereby enabling us to create classes that build upon
other classes without the problem created by multiple inheritance.
10.2 Defining Interfaces
An interface is basically a kind of class.
Interfaces contain methods and variables.
Interfaces define only abstract methods and final fields.
Interfaces do not specify any code to implement these methods and data fields contain
only constants.
The general form of an interface definition is:
interface InterfaceName
{
Variables declaration;
Methods declaration;
}
o interface is the key word and InterfaceName is a valid variable.
Variables are declared as follows:
static final type VariableName = Value;
o All variables are declared as constants.
Method declaration is as follows:
return-type methodName (parameter-list);
Eg:
interface Item
{
static final int code = 1001;
static final String name = “Fan”;
void display();
}
10.3 Extending Interfaces
Interfaces can be extended
An interface can be subinterfaced from other interfaces.
The new subinterface will inherit all the members of the superinterface.
Can be achieved using the keyword “extends”
The general form is:
interface name2 extends name1
{
Body of name2;
}
While interfaces are allowed to extend to other interfaces, subinterfaces cannot defines
the methods declared in the superinterfaces.
When an interface extends 2 or more interfaces, they are separated by commas.
An interface cannot extend classes
10.4 Implementing Interfaces
Interfaces are used as “superclasses” whose properties are inherited by classes.