Interfaces
Estimated time to read: 4 minutes
Overview¶
An interface only declares method signatures. It does not declare attributes^ , constructors, method bodies.
^ Attributes / Variables can be defined in an interface, however, they are static and final!
An interface specifies the expected behaviours in the method signatures, but does not provide any implementation details. It is basically a blueprint for a class, thus allowing reuse of abstractions. By using an interface, it ensures that all attributes are defined for the class being created.
Difference between Inheritance an Interface¶
The difference between inheritance and interface, is that inheritance extends a superclass to a subclass. Whereas an interface acts as a template to create multiple classes that conform to the same specification. In effect, a sub-type of the interface-type.
The difference between Inheritance and Interfaces is that any classes that extend from an interface, need to provide details on the implementation.
Interfaces are not a generalisation of a set of classes, Interfaces are used to describe behaviours. All that an interface contains are method signatures. They do not contain variables!
Subclasses and Interfaces¶
An interface is also used for sub-typing.
If a class implements an interface, it not only behaves like itself, but also as the interface.
Subclasses can also inherit / extend the interface to include specialised / unique attributes and methods.
Classes can implement one or more interface at a time, which allows them to have multiple types.
Interface Inheritance¶
Much like classes, interfaces can inherit from other interfaces.
However, interface inheritance should not be abused. Interfaces should not be extended simply to created a larger interface.
Interface A should only inherit from Interface B is the behaviours in Interface A can fully be used as a substitution for Interface B.
Interface Inheritance Example¶
Initial Interface¶
The below interface describes the possible movement behaviours / methods of vehicles, such as cars and boats.
Extending an Interface¶
Suppose that the movement of a plane or a submarine need to be accounted for, ie, movement in the Z-axis. The Z-axis movement only applies to these specific objects. To solve this, a second interface can be created which inherits from IVehicleMovement
.
Interface Inheritance for Initial Interface¶
The below interface extends the initial interface, but also includes an extra method signature which is specific to this interface.
If a class is created using the interface with inheritance, it will take on the following methods:
- moveOnX()
- moveOnY()
- moveOnZ()
Meaning that all vehicles with three axis of movement can now simply use the IVehicleMovement3D
interface when their class is created.
Building Interfaces¶
Keyword¶
To describe an interface in Java, the keyword interface is used in the interfaces signature.
Well, no shit Sherlock - Tavis
Interface Example Code¶
IMPORTANT NOTE ^ Depending on implementation, an interface defined in code may or may not start with the letter 'I'.
IMPORTANT NOTE ^^ There is no need to define whether a method is public or private at this point. This is class specific and down to the implementation. The example above is technically wrong, but let's learn from it!
The above interface code example provides an interface named Animal, with three behaviours / methods. These are;
- move
- speak
- eat
Note that they have no implementation, it is up to the class that is being created from the interface to define the implementation.
All that the interface does is provide a blueprint for the class to adhere to a specification.
Using an Interface to build a Class¶
Keyword¶
To build a class from an interface, the keyword implements is used in the class signature.
This tells the class the look toward the interface for the details regarding the specification of the class methods. Remember, it is up to class to implement these!
Interface Implementation Example Code¶
public class Dog implements IAnimal{
/*
Attributes go here for example;
String name;
int age;
*/
@Overvide
public void move() {...}
@Override
public void speak() {...}
@Override
public void eat() {...}
}
The class must have all of the method signatures from the interface explicitly declared and implemented within the class.
IMPORTANT NOTE! The @Override
annotation is required to tell the Java Compiler that the method described below it, is overriding the one given in the interface / superclass.
Interfaces in UML¶
In UML, Interfaces are explicitly noted in guillemets or french quotes, <<interface>>
.
The interaction between the interface and a class that is implementing the interface is indicated using a dotted arrow with an unfilled arrow head. The arrow head points towards the interface.
Interfaces in UML example¶
The above class diagram example shows that the Dog class will implement the methods from the IAnimal interface.
Polymorphism¶
Interfaces are a means of implementing Polymorphism.
Polymorphism is when two classes have the same description of a behaviour, but the implementations of the behaviour may be different.
Polymorphism Example¶
Using the prior example, an interface can be created, IAnimal, which contains a method signature, speak()
.
If two classes are created by implementing IAnimal, Dog and Cat, both are created from the same interface, however, they can both have different implementations of the speak()
method.
Polymorphism Example Code¶
public interface IAnimal{
String Speak();
}
public class Cat implements IAnimal{
@Override
public String Speak(){
return "Meow";
}
public class Dog implements IAnimal{
@Override
public String Speak(){
return "Woof";
}
}
Overlapping method signatures¶
A class, in Java, can implement as many interfaces as needed.
As an interface is only a 'blueprint' for a class, overlapping method signatures, by way of a single implementation, from multiple interfaces is allowed. .