abstract-interface.ppt java

Post on 18-Apr-2015

232 Views

Category:

Documents

12 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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 interface.

2

• An interface is basically a kind of class—it 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 an informal way of realising multiple inheritance in Java.

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. publicinterface Relation{ public boolean isGreater(Object a, Object b);Public boolean isLess(Object a, Object b);Public boolean isEqual(Object a,Object b); }

4

public interface OperateCar {  // constant declarations, if any // method signaturesInt 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);......// more method signatures

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:

6

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 threeabstract methods above

8

• To model multiple inheritance 

• A class can implement multiple interfaces while it can extend only one class

9

• 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 • Interfaces themselves have inheritance relationship

among themselves

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! 

An interface can only be implemented by classes or extended by other interfaces

11

12

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!”);}

}

• 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 keyword extends as follows:

13

interface InterfaceName2 extends InterfaceName1 {

// Body of InterfaceName2}

• A general form of interface implementation:

• 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 restrictions or special features).

14

class ClassName extends SuperClass implements InterfaceName [, InterfaceName2, …]{

// Body of Class}

• 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:

15

Student Sports

Exam

Results

extends

extendsimplements

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 method body for each abstract method.

17

top related