Transcript Interfaces
HIT2037
Software Development in Java
Interfaces, Not User Interfaces
revised – April 30, 2010
Learning Objectives
Are able to describe what an interface is
Can explain why an interface might be used
Can implement an interface and use an interface
Can describe when interfaces should be used
Understands the role of an interface as a type
2
What
is an interface?
3
What is an interface?
Looks like a class but
All methods are abstract
No implementation for the methods
There are no constructors.
All methods are public.
All fields are public, static and final, there constants.
You can never instantiate an object from an interface.
4
Where do interfaces come from?
Programmers write interfaces
Use to Specify what a class does
Use to Specify what a number of classes will do
This ones from a java Tutorial on Interfaces
http://72.5.124.55/docs/books/tutorial/java/concepts/interface.html
5
Example Interfaces – from the API
6
Here is a simple one I wrote
Receives an Object and returns an Object
public interface ICloneable
{
public abstract Object clone();
}
7
Why
would you use an Interface?
8
Why would you implement an Interface
“There are a number of situations in software
engineering when it is important for disparate groups of
programmers to agree to a "contract" that spells out
how their software interacts.”
“Each group should be able to write their code without
any knowledge of how the other group's code is written.
Generally speaking, interfaces are such contracts.”
http://72.5.124.55/docs/books/tutorial/java/IandI/createinterface.html
9
Why would you implement an Interface
Improves design,
Encourages Strong separation of functionality from
implementation.
Clients interact independently of the implementation.
10
What
does it mean to implement an
interface?
11
Classes that implement the interface
Will have the behaviour defined by the interface
When you Implement the Interface you must supply an
implementation for each method defined by the Interface
Your class will not compile unless it has the required methods
“Interfaces are reference types that other types
implement to guarantee that they support certain
operations.”
“An interface is never directly created and has no actual
representation …”
“An interface defines a contract. A class or structure that
implements an interface must adhere to its contract.”
Source – Microsoft VB.Net, (help files)
Interfaces
If your program uses a class that implements an interface
it can
Test the class implements the interface
Have confidence that the class will have the methods defined by
the interface
Use a reference variable of the interface type to access objects
of the class
What is an interface?
Interfaces provide specification without implementation.
A class can implement many interfaces
Interfaces support polymorphism
Many different objects can implement the same interface,
They outwardly present the save methods
But perform the job in their own way.
15
How
do we show an Interface in a
class Diagram?
16
Specifying use of Interface in a Class Diagram
A class is telling the world, I will have the behaviour
defined by the Interface.
Student
ICloneable
17
How do we implement an interface?
Select the Interface
Change class definition
Add defined methods
All Methods?
18
Step 1,
Define or find the Interface you wish to implement
Make sure you understand what it does
A class that implements this interface will be able to
return a clone (copy) of itself when the clone method is
called.
interface ICloneable
{
public abstract Object clone();
}
19
Step 2
Show that the class will implement the interface
public class Student implements ICloneable
{
}
20
Step 3
Add the method signatures for each required method
that this class will implement
public class Student implements ICloneable
{
public Object clone()
{
}
}
21
Step 4
Write the method bodies
public class Student implements ICloneable
{
public Object clone()
{
Student s = new Student();
s.name = this.name;
... Set other fields here
return s;
}
}
22
Step 5
TEST your work
Declare a variable of the interface type
ICloneable i;
Assign an object
i = new Student();
Test the methods
Student other = i.clone();
Try instance of
If (other instanceof ICloneable)
23
So
where’s the polymorphism?
24
Many different types of objects can implement the same
interface
Donkey, Lecturer, Student could all implement
ICloneable
Each one of these would then have an IClone method
Each one of these would be seen to be ICloneable
Each one could be added to a collection of ICloneables
Each one will clone in its own way
25
When should we use Interfaces?
When a class provided by Java tells you it requires objects used by it to
implement an interface.
When we want to deal with a collection of classes at a particular level of
abstraction.
We might want all these classes to be ISaveable
Have a method SaveMe
When we want confidence that a class has certain behaviour
e.g. serializable
Interfaces are a like in Inheritance
So far we have looked at using inheritance to combine
groups of classes related by classification.
Student is a kind of Person
Lecturer is a kind of Person
Interfaces implement inheritance in terms of
functionality. All classes that implement an interface
have the functionality of the interface.
Person is Comparable with another Person
Cost is Comparable with another Cost
Molecule is Comparable with another Molecule
Inheritance and Interface together?
I do Bicycle things
I am a Bicycle
I am a Fox
public class Fox extends Animal implements Drawable
{
...
I am a Drawable
}
I have the behaviour defined by the
Actor and the Drawable interfaces
I am an Animal
public class Hunter implements Actor, Drawable
{
...
}
I am an Actor
I am a Drawable
28
Reading & Exercises
Page 328 – 336
Complete the java tutorial
http://www.j2ee.me/docs/books/tutorial/java/IandI/createinterface.html
29
References
http://72.5.124.55/docs/books/tutorial/java/concepts/interface.html
http://72.5.124.55/docs/books/tutorial/java/IandI/createinterface.html
http://www.math.vu.nl/~eliens/documents/java/tutorial/java/more/interfaces.html
Worth reading
http://mail.python.org/pipermail/python-list/2007-March/603412.html
30