Download Interfaces - Ohio State Computer Science and Engineering

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSE 2123
Object-oriented Programming:
Interfaces
Jeremy Morris
1
Interfaces


Recall: The public interface is the methods
that a class presents to the world to use
The Java language has a related idea as a
language construct – the interface



An interface specifies the behavior for a class
Interfaces are a kind of superclass
All classes that implement an interface guarantee
that they offer the methods that the interface
advertises
2
Interfaces

Think of an interface as a contract


The interface provides a list of methods
A class that implements the interface guarantees
that it implements those methods
3
Interfaces - Example

For example, the List interface

Advertises a number of methods that all classes that
operate as must have, including:
E get(int index)
boolean isEmpty()
ListIterator listIterator()
int size()

Any new class that you want to write that
implements the List interface must include
implementation of these methods (among others)
http://docs.oracle.com/javase/6/docs/api/java/util/List.html
4
Using Interfaces

When you use a class that implements an
interface you have a choice

You can declare it as you would an object of that
class:
ArrayList<Integer> myList = new ArrayList<Integer>();

Or you can declare it using the name of the
interface as the reference:
List<Integer> myList = new ArrayList<Integer>();
5
Why Interfaces?

Any classes that implement the same
interface can be used interchangeably

Example: LinkedList and ArrayList both
implement the List interface

Suppose you have a situation where you have
processing that sometimes is faster using an
ArrayList and sometimes is faster using a
LinkedList


You could write and maintain two different programs – one
for each scenario
Or you could exploit the fact that they both implement the
List interface
6
Using Interfaces - Example
public static int myMethod(List<Integer> myList) {
…
}

The above method will work for any class that
implements the List interface


We could call with an ArrayList or a LinkedList as
a parameter
Both implement the List method


Note that that means that myMethod can only use
methods that are in the List interface
Cannot use methods specific to LinkedList or ArrayList
in this method, only the methods common to the List
interface
7
Interface Warning - Polymorphism

When you use an interface to declare a reference (i.e. on
the left side), only the methods provided by that interface
are available to your object

Java considers the object to be of the interface type

only methods available to the interface are available to the object
List<Integer> myList = new LinkedList<Integer>();



has a method named addFirst()
The List interface does not include a method
named addFirst()
You will not be able to use the addFirst() method
with myList as declared above!
LinkedList
8
Interfaces – Another Example

The Collection interface

The Collection interface advertises methods like:
boolean add(E element)
int size()
Iterator<E> iterator()

The Collection interface is implemented by:

ArrayList
LinkedList
HashSet

… and many other classes (see the docs for more)


http://docs.oracle.com/javase/6/docs/api/java/util/Collection.html
9
Interfaces – Another Example

How does implementing the Collection
interface help?

Look at the ArrayList API:


http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html
The addAll(Collection< … > c) method


Pass anything that implements the Collection interface
to the method
Elements in that collection are added to the ArrayList


In a specific order for ordered collections, or no guaranteed
order for non-ordered collections
Using interfaces makes our programs more flexible and
more portable
10
Interfaces – Multiple Interfaces

A single class can implement multiple
interfaces

ArrayList implements:


LinkedList implements:


Iterable, Collection, List, RandomAccess, …
Iterable, Collection, List, Queue, Deque, …
HashSet implements:

Iterable, Collection, Set
11
Using Interfaces

We can also create our own interfaces

Imagine a whole variety of Student records:




UgradStudent
GradStudent
ContEdStudent
Each of these should provide the same
functionality, but internally might do things
differently


Compute grades
Compute fees/tuition
12
Using Interfaces
 We could create a Student interface:
public interface Student {
public void setFirstName(String firstName);
public void setLastName(String lastName);
public void setStudentId(String studentId);
…
}

Interface defines all of the method signatures
for classes that implement it


Does not define the methods, just the signatures
(abstract class)
Does not define the member variables either
13
Using Interfaces

When we want to write a class that
implements an interface, we use the
implements keyword:
public class UgradStudent implements Student {
private String firstName;
private String lastName;
…
public void setFirstName(String firstName) { … }
}

We must define all of the methods defined by
the interface
14
Using Interfaces

Our class can also implement multiple
interfaces:
public class UgradStudent implements Student,
GradeEntry {
private String firstName;
private String lastName;
…
public void setFirstName(String firstName) { … }
…
public double getLabGrade(int index) {…}
}

Here we must implement the methods in both the
Student and GradeEntry interfaces
15
Using Interfaces

Once we’ve created our new classes, we can
declare them using our interface:
Student student1=new UgradStudent();
Student student2 = new GradStudent();

Both of these are considered Student objects
by the program


Any method advertised by the Student interface is
available to these objects
Warning! Only methods advertised by the
Student interface is available to these objects!
16
Using Interfaces

Only methods advertised by the Student
interface is available to these objects!
Student student1=new UgradStudent();

If the UgradStudent class has methods that are
not defined in the Student interface, you cannot
access them

student1 is considered an object of type Student

Only methods available to objects of type Student are
available to student1
17