Chapter 7 Interfaces
Chapter 7 Interfaces
Interfaces
Interfaces Introduction
<<Interface>>
Speaker
speak()
interface InterfaceName
{
• Example: // Constant/Final Variable Declaration
// Methods Declaration – no method
body
}
interface Speaker
{
public void speak( );
}
Implementing Interfaces
• Interfaces are used like super-classes whos properties are
inherited by classes. This is achieved by creating a class that
implements the given interface as follows:
extends
Exam
implements
extends
Results
Software Implementation
class Student
{
// student info. and access methods
}
interface Sports
{
// sports grace marks (say 5 marks) and abstract methods
public static final int Sports_Grace_Marks = 5;
void calPerformance();
}
class Exam extends Student
{
// example marks (test1 and test 2 marks) and access methods
}
class Results extends Exam implements Sports
{
// implementation of abstract methods of Sport interface
// other methods to compute total marks = test1+test2+sports_grace_marks;
// other display or final results access methods
}
Interfaces and Software Engineering
• Interfaces, like abstract classes and methods, provide templates
of behaviour that other classes are expected to implement.