Professional Documents
Culture Documents
7.1 Java Lecture88 PDF
7.1 Java Lecture88 PDF
Polymorphism
PRESENTED BY
DatabaseStorage FileStorage
MySQLStorage XMLStorage
OracleStorage JSONStorage
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.
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);
icemalta.com
System.out.println(d.getDescription());
}
}
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