Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Lesson 88

Polymorphism

PRESENTED BY

Keith Vassallo icemalta.com


Polymorphism via Class Inheritance
- Consider the following class diagram:
Storage

DatabaseStorage FileStorage

MySQLStorage XMLStorage

OracleStorage JSONStorage

- Each of the subclasses can fulfil the requirements of the parent.

icemalta.com
- Hence, for example, OracleStorage can polymorphically act as both
DatabaseStorage and Storage.
Polymorphism via Implementing
Interfaces
- Any class implementing an interface must implement the methods
specified in the interface.
- So, classes can polymorphically fulfil the needs of the interface they are
implementing.
<<Interface>>

Printable

icemalta.com
XMLDocument TrainTicket
Polymorphism in Code
Consider the following class diagram and code.

Animal Dog d = new Dog(); The object will


only have
+ weight: double Mammal m = new Dog();
+ age: double
Animal a = new Dog();
+ breathe(): void
+ reproduce(): void methods
//Dog has methods from all the hierarchy available to its
d.bark(); specified type
Mammal d.giveLiveBirth();
+ furLength: double
+ rearYoung(): void d.breathe(); and superclasses.
+ giveLiveBirth(): void
//Mammal only has methods from itself and its parents
m.giveLiveBirth();
Dog m.breathe();

icemalta.com
+ pedigree: String
+ bark(): void
+ wagTail(): void //Animal only has methods from itself
a.breathe();
Generic Methods
- The use of Polymorphism allows us to write very generic methods.
- Let’s take two very different classes - a DatabaseConnector, and a Cow!

public interface Describeable { public class DatabaseConnector implements Describeable { public class Cow implements Describeable {
String getTitle(); private String host; private String name;
String getDescription(); private String username; private int age;
} private String password; private String farm;
private boolean connected = false;
public Cow(String name, int age, String farm) {
public DatabaseConnector(String host, String username, String password) { this.name = name;
this.host = host; this.age = age;
this.username = username; this.farm = farm;
this.password = password; }
connected = true;
} @Override
public String getTitle() {
@Override return name + " the cow";
public String getTitle() { }
String status = connected == true ? "Online" : "Offline";

icemalta.com
return host + " " + status; @Override
} public String getDescription() {
return name + " is a " + age + " year old cow living on " + farm;
@Override }
public String getDescription() { }
return host + ": " + username + " " + password;
}
}
Generic Methods
Since both classes implement the same interface, and that interface has the
getTitle() and getDescription() methods, we can ensure that both classes also
have those methods.
public class SomeClass {
public static void main(String[] args) {
SomeClass m = new SomeClass();
Cow betsy = new Cow("Betsy", 4, "Primrose Farm");
DatabaseConnector dbc = new DatabaseConnector("localhost", "root", "password");
m.describe(betsy);
m.describe(dbc);

public void describe(Describeable d) { Generic method


System.out.println(d.getTitle());

icemalta.com
System.out.println(d.getDescription());
}
}
Great work, you’ve completed this lesson!

Next up: Homework 10: A Fully


Object-Oriented Application.

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