abstract-interface.ppt java

17
1

Upload: aishwarya-jain

Post on 18-Apr-2015

225 views

Category:

Documents


12 download

TRANSCRIPT

Page 1: Abstract-Interface.ppt Java

1

Page 2: Abstract-Interface.ppt Java

• 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

Page 3: Abstract-Interface.ppt Java

• 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

Page 4: Abstract-Interface.ppt Java

// 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

Page 5: Abstract-Interface.ppt Java

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

Page 6: Abstract-Interface.ppt Java

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

Page 7: Abstract-Interface.ppt Java

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

Page 8: Abstract-Interface.ppt Java

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

Page 9: Abstract-Interface.ppt Java

• To model multiple inheritance 

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

9

Page 10: Abstract-Interface.ppt Java

• 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

Page 11: Abstract-Interface.ppt Java

• 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

Page 12: Abstract-Interface.ppt Java

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

}

Page 13: Abstract-Interface.ppt Java

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

Page 14: Abstract-Interface.ppt Java

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

Page 15: Abstract-Interface.ppt Java

• 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

Page 16: Abstract-Interface.ppt Java

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

Page 17: Abstract-Interface.ppt Java

• 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