Interfaces Flashcards
Limits of Inheritance Polymorphism
Java is a single inheritance language
⚫ Classes can extend precisely one other class. No more.
⚫ So what if we want to build a class capable capable of more than one role?
⚫ As an abstract example:
Man and a Woman are both arguably subclasses of Person
A Lecturer is a Person, but will also likely be a Man or Woman
Try implementing that in an inheritance hierarchy without duplication!
⚫ Some other languages are multiple inheritance, and can have an unlimited
number of parent classes (e.g. C++).
⚫ By polymorphism instances can be treated as a type of any parent class.
⚫ This provides additional flexibility, but brings complexity.
⚫ How to ratify multiple definitions and implementations of functionality from multiple parent classes? And how does this pass on to subsequent subclasses?
Interfaces: Motivation
Separating Polymorphism from Inheritance
⚫ Inheritance guarantees that a subclass of a class has all the external capabilities (visible methods and instance variables) of a superclass.
⚫ This means polymorphism can occur without compromising type safety.
⚫ Need inheritance be the only way to guarantee a class has specific
capabilities?
Interfaces: Definition 1
Interfaces
⚫ An Interface is an abstract specification of zero or more methods.
⚫ Interfaces cannot contain any instance variables.
⚫ Interfaces cannot contain any method implementation.
⚫ They can be considered similar to an abstract class, but without any
functionality.
⚫ A class can elect to implement any number of interfaces.
⚫ By contract, that class must then implement the methods specified in the
interface. This is enforced at compile time.
⚫ We can therefore guarantee that any class implementing an interface has
all the methods defined in that interface…
Interfaces: Definition 2
Interfaces and Polymorphism
⚫ Polymorphism in Java is also applied to interfaces.
⚫ As we can definitively say a class has the capabilities defined in an interface,
we can apply polymorphism without compromising type safety.
⚫ In other words, a class can be treated as an ‘instance’ of any interface it
chooses to implement.
⚫ As classes can implement many interfaces, this provides a clean mechanism that addresses the limitations of single inheritance languages, whilst sidestepping the complexity of multiple inheritance.
⚫ A complete definition of Polymorphism in Java :
⚫ A class can be treated as a type of its class, any of its super classes, or any of the interfaces it implements.
Interface Example
The Java API contains a multitude of interfaces…
⚫ Consider Swing’s ActionListener interface as a familiar example.
⚫ This specifies an interface with a single method:
Interfaces and Inheritance
Interfaces also have an inheritance hierarchy
⚫ Interfaces can extend other interfaces.
⚫ Methods defined in a super-interface are included in all sub-interfaces
⚫ Polymorphism is transient, as with object inheritance. An abstract example:
However:
⚫ The interface hierarchy is totally distinct from the class hierarchy.
⚫ An interface cannot extend a class.
⚫ A class cannot extend an interface.
AbstractButton: addActionListener
Consider what is happening here.
⚫ This method (in AbstractButton) takes a single parameter – an object reference to an instance that implements the ActionListener interface.
⚫ It can then store these (in a heterogeneous collection), until needed.
⚫ It can then invoke the actionPerformed method on those instances when the
time is right (in this case, the button is clicked).
⚫ AbstractButton had no knowledge of our class (ChessBoard) when it was written, yet can invoke methods on it in a type safe manner!
Roll your own interfaces
It is very straightforward to create your own interfaces
⚫ Java interfaces can be used anytime you want to promote polymorphism, but
don’t want to be restricted by inheritance.
⚫ Promotes extensibility and elegance.
⚫ Allows you to define what other programmers must do in order to interface
with your code. Then your code can support working with objects that haven’t
even been written yet.
Remember:
⚫ Interfaces define only methods. No instance variables allowed
⚫ Interfaces are merely a specification. No implementation of the methods.
⚫ If you need to provide default behaviour, use inheritance*
.
Example custom interface: Creation
Interfaces are defined much like classes
⚫ Interfaces are named, in capitalized camel case.
⚫ Filename should match interface name.
Example custom interface: Usage
Classes use implements keyword to implement your interface.
⚫ Can then be treated as a type of your new interface.