0% found this document useful (0 votes)
34 views

5.1 Java Lecture86 PDF

Uploaded by

tedyoung
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

5.1 Java Lecture86 PDF

Uploaded by

tedyoung
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lesson 86

Interfaces

PRESENTED BY

Keith Vassallo icemalta.com


Interfaces
- An interface is a structure that specifies a common API – Application
Programming Interface.
• The API of a class consists of those public fields and methods available
to programmers.
• An interface defines a list of abstract methods.
• Any class implementing the interface must then implement these
methods.
• In essence, an interface ensures that implementing classes will have
certain methods.

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>>

Ink Paper SparePaint Printer Laptop

- SparePaint must be labelled with the room it was used to paint.

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;
}
}

- The SparePaint class implements the interface.


- This means it must override the getLabelContent() method.

icemalta.com
Implementing Interfaces
public abstract class Equipment implements Labelable{
public void powerOn() {
System.out.println("Equipment on");
}

public void powerOff() {


System.out.println("Equipment off");
}
}

- 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;
}
}

- The Laptop class must implement the getLabelContent() method since it


is not abstract.

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();
}

- The SparePaint class also needs to implement this interface.

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"
}
}

We specify the interfaces to implement, separating with commas.

icemalta.com
-
- We then implement all relevant methods.
Great work, you’ve completed this lesson!

Next up: Encapsulation with Getters


and Setters.

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

You might also like