You are on page 1of 17

1

Interface is a conceptual entity similar to a Abstract class. Can contain only constants (final variables) and abstract method (no implementation) - Different from Abstract classes. Use when a number of classes share a common interface. Each class should implement the
2

An interface is basically a kind of classit contains methods and variables, but they have to be only abstract classes and final fields/variables. Therefore, it is the responsibility of the class that implements an interface to supply the code for methods. A class can implement any number of interfaces, but cannot extend more than one class at a time. Therefore, interfaces are considered as
3

// Note that Interface contains just set of method // signatures without any implementations.

// No need to say abstract modifier for each method // since it assumed. Relation{

publicinterface

public boolean isGreater(Object a, Object b); Public boolean isLess(Object a, Object b);
4

public interface

OperateCar {

// constant declarations, if any // method signatures Int turn(Direction direction, double radius, double startSpeed, double endSpeed); Int changeLanes(Directiondirection, double startSpeed, double endSpeed); Int signalTurn(Direction direction, boolean signalOn); Int getRadarFront(double distanceToCar, double speedOfCar); Int getRadarRear(doubledistanceToCar, double speedOfCar); 5

Interfaces are used like super-classes who properties are inherited by classes. This is achieved by creating a class that implements the given interface as follows:
class ClassName implements InterfaceName [, InterfaceName2, ] { // Body of Class }

To reveal an object's programming interface (functionality of the object) without revealing its implementation This is the concept of encapsulation

The implementation can change without affecting the caller of the interface
The caller does not need the implementation at the compile time It needs only the interface at the compile time During runtime, actual object instance is associated with the interface type
7

To have unrelated classes implement similar methods (behaviors)

One class is not a sub-class of another Example: Class Line and class MyInteger They are not related through inheritance You want both to implement comparison methods

checkIsGreater(Object x, Object y) checkIsLess(Object x, Object y) checkIsEqual(Object x, Object y) Define Comparison interface which has the three abstract methods above
8

To model multiple inheritance A class can implement multiple interfaces while it can extend only one class

All methods of an Interface are abstract methods while some methods of an Abstract class are abstract methods Abstract methods of abstract class have abstract modifier An interface can only define constants while abstract class can have fields Interfaces have no direct inherited relationship with any particular class, they are defined independently
10

The methods of an Interface are all abstract methods They cannot have bodies You cannot create an instance from an interface

For example: PersonInterface pi = new PersonInterface(); //ERROR!


11

class Politician implements Speaker { public void speak(){ System.out.println(Talk politics); } }

class Priest implements Speaker { public void speak(){ System.out.println(Religious Talks); } }

class Lecturer implements Speaker { public void speak(){ System.out.println(Talks Object Oriented Design and Programming!); } }
12

Like classes, interfaces can also be extended. The new sub-interface will inherit all the members of the superinterface in the manner similar to classes. This is achieved by using the interface InterfaceName2 extends InterfaceName1 keyword extends as follows:
{ } // Body of InterfaceName2

13

A general form of interface implementation:


class ClassName extends SuperClass implements InterfaceName [, InterfaceName2, ] { // Body of Class }

This shows a class can extended another class while implementing one or more interfaces. It appears like a multiple inheritance (if we consider interfaces as special kind of classes with certain
14

Consider a university where students who participate in the national games or Olympics are given some grace marks. Therefore, the final marks awarded = Exam_Marks + Sports_Grace_Marks. A class diagram representing this scenario is as follow:
Student extends Exam extends Results
15

Sports

implements

class Student { // student no and access methods } interface Sport { // sports grace marks (say 5 marks) and abstract methods } class Exam extends Student { // example marks (test1 and test 2 marks) and access methods } class Results extends Exam implements Sport { // 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 }

16

Data and methods may be hidden or encapsulated within a class by specifying the private or protected visibility modifiers. An abstract method has no method body. An abstract class contains abstract methods. An interface is a collection of abstract methods and constants. A class implements an interface by declaring it in its implements clause, and providing a
17