©
Copyright 2002, 2003, 2004 by Daniel Berleant
![]()
![]()
1. An interface is “almost” a class but doesn’t get all the
way there
2. Defining a class:
public class
MyClass
3. Defining an interface:
public interface
MyInterface
4. A class can contain method
definitions
Even an abstract class can contain
definitions
5. An interface can only contain
1. method “prototypes”
…or in Java-ese, “abstract
methods”
2.
and final
variables
…Java-ese for
constants
final int
myNum=6;
1. Example:
public interface
MouseListener{
public void
mouseClicked(MouseEvent event);
public void
mouseEntered(MouseEvent event);
public void mouseExited(MouseEvent
event);
public void
mousePressed(MouseEvent event);
public void
mouseReleased(MouseEvent event);
}
2. Since an interface doesn’t define the methods –
·
You must
define them when using the interface
3. How to use the interface? Example:
class SuperLight extends
DeluxeLight
class Cat implements
MouseListener
class MyClass extends
yourClass implements MouseListener, KeyListener
4. Note
MyClass
inherits from yourClass
5.
MyClass can inherit only from one base class
(yourClass)
(Unlike C++, which allows complicated
inheritance networks)
6.
But Java gets
around that by augmenting inherited stuff with implemented
stuff
MyClass
inherits from
one class and
implements from
two other entities
These
entitities are called interfaces
Two
predefined interfaces:
MouseListener
and
KeyListener
7. Java: you can only inherit directly from one class
Though inheritance is transitive, so
it also inherits whatever its superclass inherited
earlier
(from its own superclass)
8. But: you can implement any number of
interfaces
9.
implements is
a
controlled way to circumvent the inherit-from-one rule
(More
controlled than allowing inheritance from N classes)
10.
One of the ways it
controls:
·
If you
implements it then you must implement its
methods
·
Thus: an interface
provides prototypes (“abstract methods”)
·
You must define
(implement) the code for those prototypes/abstract methods
11.
MyClass must now provide ALL the definitions
for…
… methods prototyped in MouseListener and KeyListener
![]()
That is its
superclass
class SuperLight extends
DeluxeLight
13.
An interface can
build upon any number of other interfaces
1. it
then contains its own prototypes + theirs too
2. built-upon interfaces are called
superinterfaces
– by
analogy to superclasses
3. note
an interface can’t implement anything
4. because they don’t have methods,
(only abstract methods/prototypes)
(and final variables/constants)
import java.awt.event.*;
interface RatListener extends
MouseListener,
KeyListener{
...
}
but
class MazeRunner implements
RatListener{...
(it makes no sense for a class to extend an interface), or
class MazeRunner extends
Animal implements RatListener
Is Animal a class or
an interface?
14.
First, define (or
import) the new interface:
import
java.awt.event.*;
public interface
StockWatcher{
void
valueChanged(
String
stockName,
double
price); //the “;” indicates it’s a
prototype
}
15.
Now, anything that
implements StockWatcher
must define (implement)
valueChanged(
)
class MyStocks extends
Stocks implements StockWatcher{
void valueChanged(String s, double
p){
System.out.println(
“Checking if s has doubled”
+“ or halved since yesterday”);
}
}
Can we change the name
valueChanged(
) to something
else?
16.
Next, process
something with
a
valueChanged(String,double) method
public class
MyClass{
public void main(String args[
]){
MyStocks m=new MyStocks(
);
StockWatcher s=new MyStocks(
);
String
whichStock=”MRCH”;
double
priceChange=getChange(whichStock);
s.valueChanged(whichStock,
priceChange);
}
double
getChange(String S){...}
}
17.
Notes:
·
s is a reference type (handle, pointer
variable)
·
MyStocks...implements
StockWatcher
·
Therefore you know
that
·
s has a valueChanged(...)
method
·
More generally,
s can be set to a
new object of any class
…that implements StockWatcher
…and s will have a valueChanged(...)
method
·
In essence, by tagging pointer
s
as a StockWatcher
·
... you are saying
it has a valueChanged(...)
method
·
A handle/pointer/reference can expect to
indicate
- a
class, OR
-
an interface
- both can be useful
- both are illustrated above
![]()
18.
If you add a new
method prototype to an interface
19.
…will classes that
implement the interface still compile?
20.
If instead of
adding new prototypes to an interface, you
declare a new interface with the new
prototype
21.
Will anything not
compile now?
![]()
Avoiding
having to implement ALL the interface methods
There are classes called
MouseAdapter
KeyAdapter
WindowAdapter
etc.
These classes provide default (“do nothing”)
implementations
Now
you can ...extends
MouseAdapter...
Instead of
...implements
MouseListener...
This is useful when you don’t want to
-
implement
everything in MouseListener
-
(i.e. you only want to
implement some of the methods)
How would you write a
class MouseAdapter...
...given an interface
public interface
MouseListener{
public void
mouseClicked(MouseEvent m);
public void
mouseEntered(MouseEvent m);
public void mouseExited(MouseEvent
m);
public void
mousePressed(MouseEvent m);
public void
mouseReleased(MouseEvent m);
}
?????
22.
Question: could you
always extend MouseAdapter instead of implementing MouseListener?
![]()
Basic
idea:
Declare an anonymous inner class extending an
adapter
Make an object from it
Note the containing class can extends something else
Good because a class can only extend one other
class
Now we can use an adapter and extend another class
too
Syntax:
class SuperLight extends
TrafficLight{
...
myObject.addWindowListener(
new WindowAdapter( ){
<override desired methods here>
} //end of anonymous inner class
);
//finish adding the listener
...
}