Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interfaces Describe what classes should do, without specifying how they should do it Not a class, but a set of requirements for classes that want to conform.

Similar presentations


Presentation on theme: "Interfaces Describe what classes should do, without specifying how they should do it Not a class, but a set of requirements for classes that want to conform."— Presentation transcript:

1 Interfaces Describe what classes should do, without specifying how they should do it Not a class, but a set of requirements for classes that want to conform to the interface

2 Views of an Interface The interface between an ADT and the rest of a system can be viewed from two sides: –From the ADT side, it specifies the features exported to the rest of the system. –For the rest of the system, it specifies the features imported from the ADT. These are subtly different (for reasons related to the theory of polymorphism).

3 Practical consequences A programming language should provide ways of describing both views of an interface Ideally the same mechanism should do both, but unless the language supports polymorphism properly, this is very difficult to arrange – Java does not achieve it.

4 What Java does provide a mechanism to define the import view, which it calls an interface restrict this (because it does not support polymorphism) handle the export views of ADTs in other ways.

5 Interfaces An interface is like a class, but... –it is declared with the word interface –it contains only methods, not variables –may define constants –the methods are all public, whether you say so or not –the methods are only declared, not defined

6 Implementations You extend a class, but you implement an interface A class can only extend one other class, but it can implement many interfaces The interfaces are comma-separated: class MyClass extends MySuperclass implements Iface1, Iface2 {...

7 Example interface interface Iterator { boolean hasNext( ); Object next( ); void remove( ); } Again, these methods are all automatically public

8 Example implementation class MyClass extends MySuperclass implements Iterator { public boolean hasNext( ) {... code... } public Object next ( ) {... code... } }

9 Complex Number Interface and Implementation Demo

10 Promises When you implement an interface, you promise to provide code for all the methods that the interface declared This means that your code guarantees certain behavior (or at least certain methods!) If you don't implement all the methods, you have an abstract class

11 Abstract Classes If a class is abstract, you have to say so: abstract class AlmostAnIterator implements Iterator {... You cannot create an instance (object) of an abstract class......but you can extend an abstract class (and hopefully fulfill the rest of your promises!)

12 Abstract Classes C an define variables and methods that the inheriting classes will have Cannot define their values, Cannot define a constructor. Abstract classes are not much used

13 Java Interfaces The Java interface mechanism extends this: it allows a form of multiple inheritance – ie a real class may implement several interfaces.

14 Limitations of Java Interfaces To make its interface mechanism work, Java imposes the following restrictions. 1.An interface cannot define any Java constants - ie public final variables, because they would have to belong to some object of the interface class; and an interface is a form of abstract class; and abstract classes cannot have objects belonging to them. (Can have a public static final constant)

15 Limitations of Java Interfaces 2.An interface is not allowed to define a (Java) constructor, because an interface is a form of abstract class; and an abstract class cannot define a constructor. 3.An interface cannot define any class (ie static) methods or variables, because Java maintains an object (of class Class) for every actual class static methods or variables are part of this class; but abstract classes or interfaces do not have such “class objects”.

16 Practical Consequences A Java interface is effectively useless for defining the “export view” of the class for some ADT Some object oriented languages provide a template mechanism for defining the export view of an ADT without these restrictions – a future version of Java may provide such a mechanism.

17 Java interfaces Java provides many interfaces as well as many classes All Listeners are defined as interfaces; you write their implementations An Adapter class is a convenience class that implements an interface, but all of the method bodies are empty

18 MouseInputListener interface MouseInputListener { void mouseClicked(MouseEvent e); void mouseDragged(MouseEvent e); void mouseEntered(MouseEvent e); void mouseExited(MouseEvent e); void mouseMoved(MouseEvent e); void mousePressed(MouseEvent e); void mouseReleased(MouseEvent e); }

19 MouseInputAdapter class MouseInputAdapter implements MouseInputListener { public void mouseClicked(MouseEvent e) { }; public void mouseDragged(MouseEvent e) { }; public void mouseEntered(MouseEvent e) { }; public void mouseExited(MouseEvent e) { }; public void mouseMoved(MouseEvent e) { }; public void mousePressed(MouseEvent e) { }; public void mouseReleased(MouseEvent e) { }; }

20 Why Adapter classes are convenient You can either: –class MyClass implements MouseInputListener {... define all seven methods... } Or: –class MyClass extends MouseInputAdapter {... define the methods you care about... }

21 The End


Download ppt "Interfaces Describe what classes should do, without specifying how they should do it Not a class, but a set of requirements for classes that want to conform."

Similar presentations


Ads by Google