5.1 Java Lecture86 PDF
5.1 Java Lecture86 PDF
Interfaces
PRESENTED BY
icemalta.com
- Interfaces can have static and default methods, but we won’t be covering
these advanced topics at this level.
What’s the Point?
- Imagine you have an inventory control system, with a class representing
every type of item you have in your inventory.
- Some items in your inventory will need to have some information printed
on a label.
- You decide that each labelable class will return a String with the label
content.
- However, it is not appropriate to create a super-class for all items in
inventory, since:
- They don’t all need labels.
- It doesn’t make sense to group items so different from each other.
icemalta.com
Designing the Hierarchy
<<interface>>
Labelable
getLabelContent()
<<implements>>
<<abstract>> <<abstract>>
Consumable Equipment
<<implements>>
icemalta.com
- All equipment must have a labelled, so the super-class it made to
implement the interface.
Implementing Interfaces
public interface Labelable {
String getLabelContent();
}
- Note how we do not need to make the method public and abstract.
- All interface methods are public and abstract by default.
icemalta.com
Implementing Interfaces
public class SparePaint implements Labelable {
private String roomPainted;
@Override
public String getLabelContent() {
return "Used in room: " + roomPainted;
}
}
icemalta.com
Implementing Interfaces
public abstract class Equipment implements Labelable{
public void powerOn() {
System.out.println("Equipment on");
}
- Since the equipment class is abstract, it does not need to implement the
interface.
- However, its children will have to.
icemalta.com
Implementing Interfaces
public class Laptop extends Equipment {
private String manufacturer, model;
private int yearOfManufacture;
@Override
public String getLabelContent() {
return yearOfManufacture + " " + manufacturer + " " + model;
}
}
icemalta.com
Multiple Interfaces
- As Java is a single-inheritance language, a class can only have one super-
class.
- However, it can implement multiple interfaces.
- Consider the following interface.
public interface Expirable {
String getExpiryDate();
}
icemalta.com
Multiple Interfaces
public class SparePaint implements Labelable, Expirable {
private String roomPainted;
@Override
public String getLabelContent() {
return "Used in room: " + roomPainted;
}
@Override
public String getExpiryDate() {
return "12/12/2025"
}
}
icemalta.com
-
- We then implement all relevant methods.
Great work, you’ve completed this lesson!
icemalta.com
© 2011-2017 Institute of Computer Education Ltd.
The contents of this document are copyright to the Institute of Computer Education Ltd, unless otherwise stated, and must not be reproduced without permission.
Every effort has been made to trace all of the copyright holders, but if any have been inadvertently overlooked the Institute of Computer Education Ltd. will be pleased to make the necessary arrangements at the first
opportunity. Please contact us directly. While the Institute of Computer Education Ltd. has taken all reasonable care in the preparation of this work, the Institute of Computer Education Ltd. makes no representation,
express or implied, with regard to the accuracy of the information contained in this work and cannot accept any legal responsibility or liability for any errors or omissions from the work or the consequences thereof.
The reader assumes sole responsibility for the selection of these materials to achieve its intended results. Products and services that are referred to in this work may be either trademarks and/or registered
trademarks of their respective owners. The editors and author/s make no claim to these trademarks. icemalta.com