Software Construction: Interfaces
Cards to learn interfaces.
Cards to learn interfaces.
-
- 1 / 22
-
Flashcards
Interface (broad Definition)
An interface to a class consists of the methods of that class that are accessibble to another class (e.g. it's public methods in case of an interface fused with the class definition).
When do we separate interface & class?
When different implementations are possible.
Interfaces in Java
In Java, interfaces provide a specification of the methods that it should be possible to invoke on the objects of a class.
What are abstract methods?
Methods that do not have an implementation.
Examples:
- Card draw();
- boolean isEmpty();
The "implements" keyword
Formal guarantee, that instances of class types will have concrete implementationa for all methods in the interface types.
Subtype relationship between the implementing class and the interface type (e.g. class Deck is of type CardSource).
Example: public class Deck implements CardSource { … }
When do I need a seperate interface & what should this interface specify?
There are no general answers to these questions.
The Comparable Interface (Definition)
The library does not know how want to sort our cards (It can not anticipate all the possible objects a programmer may create).
The Comparable<T> interface helps to solve this problem by defining a method on how to compare objects.
To allow us to sort a list of cards, we have to provide the expected comparable behaviour. We declare it with the implements keyword.
Function Objects (Definition)
Function objects are objects that provide the implementation for a single method.
Their interface typically maps one-to-one to that of an interface (normally, a Java interface represents only a subset of the complete interface of classes that implement it).
Example Function Object
In our card game, we want to implement different sorting strategies.
Modifying the code of compareTo (e.g., by using a flag to switch between different strategy) is deprecated.
A better solution is to move the comparison code to a separate object.
The solution is supported by the use of the Comparator<T> interface, which implements the method compare (as opposed to compareTo).
Problem with Example Function Objects
In some cases the information available to the class implementing the Comparator interface is enough (e.g., using the getter methods).
Sometimes, however, implementing the compare method will require access to private members.
Three possible solutions
- Using a nested class
- Through an anonymous class
- Recurring to lambda expressions
Iterator interface (Definition)
An iterator over a collection. Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
- boolean hasNext(): Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)
- E next(): Returns the next element in the iteration. Throws NoSuchElementException - if the iteration has no more elements.
- default void remove(): Removes from the underlying collection the last element returned by this iterator (optional operation).
Iterable<T> interface (Definition)
The iterable<E> interface only contracts us to one method: public Iterator<E> iterator(){} which returns an Iterator<E> to the underlying container of the implementation.
After implementing the Iterable<E> interface, i.e. with public class Deck implements Iterable<Card>{} we can iterate over the implementation, the Deck class in this case, with for(Card card: deck){}.