Page 406 - CITS - Computer Software Application -TT
P. 406
COMPUTER SOFTWARE APPLICATION - CITS
Key points:
1 An interface cannot extend classes because it would violate rules that an interface can have only abstract
methods and constants.
2 An interface can extend Interface1, Interface2.
Implementing Interface in Java with Example
An interface is used as “superclass” whose properties are inherited by a class. A class can implement one or more
than one interface by using a keyword implements followed by a list of interfaces separated by commas.
When a class implements an interface, it must provide an implementation of all methods declared in the interface
and all its super interfaces.
Otherwise, the class must be declared abstract. The general syntax of a class that implements an interface is as
follows:
Syntax:
1 accessModifier class className implements interfaceName
{
// method implementations;
// member declaration of class;
}
2 A more general form of interface implementation is given below:
accessModifier class className extends superClass implements interface1, interface2,.. .
{
// body of className.
}
This general form shows that a class can extend another class while implementing interfaces.
Key points:
1 All methods of interfaces when implementing in a class must be declared as public otherwise, you will get a
compile-time error if any other modifier is specified.
2 Class extends class implements interface.
3 Class extends class implements Interface1, Interface2…
The implementation of interfaces can have the following general forms as shown in the below figure.
393
CITS : IT&ITES - Computer Software Application - Lesson 109 - 115 CITS : IT&ITES - Computer Software Application - Lesson 109 - 115