Abstract Classes and Interfaces

Download Report

Transcript Abstract Classes and Interfaces

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
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).
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.
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.
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
–
–
–
–
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 { ...
Example interface
interface Iterator
{
boolean hasNext( );
Object next( );
void remove( );
}
• Again, these methods are all automatically
public
Example implementation
class MyClass extends MySuperclass
implements Iterator {
public boolean hasNext( ) {
... code ...
}
}
public Object next ( ) {
... code ...
}
Complex Number Interface and
Implementation
• Demo
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
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!)
Abstract Classes
• Can define variables and methods that
the inheriting classes will have
• Cannot define their values,
• Cannot define a constructor.
• Abstract classes are not much used
Java Interfaces
The Java interface mechanism extends this:
• it allows a form of multiple inheritance – ie
• a real class may implement several interfaces.
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)
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”.
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.
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
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);
}
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) { };
}
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 ... }
The End