Interfaces and Abstract Classes
CS 240 – Advanced Programming Concepts
Polymorphism
Polymorphism
Simple Polymorphism Examples
�Result in Memory
Person emp = new Employee();
Although they still exist in memory, ‘hireDate’ and ‘salary’ cannot be accessed from the ‘emp’ reference
Reasons for Polymorphism
Citations
1. Diagrams created by course authors: Ken Rodham and Jerod Wilkerson.
Polymorphism Example Part 1
The City Simulation Example
City Simulation Example
Vehicle Inheritance Hierarchy
Simulation Example 1
public class CitySimulation {
public void run() {
Vehicle [] vehicles = new Vehicle[6];
vehicles[0] = new Car();
vehicles[1] = new Car();
vehicles[2] = new Truck();
vehicles[3] = new Truck();
vehicles[4] = new Boat();
vehicles[5] = new Airplane();
for(int i = 0; i < vehicles.length; i++) {
vehicles[i].go()
}
}
}
Simulation Example Problems
Citations (Polymorphism Example Part 1)
1. Diagrams created by course authors: Ken Rodham and Jerod Wilkerson.
Polymorphism Example Part 2
Abstract Classes
Polymorphism Example Problems
Abstract Vehicle Class
public abstract class Vehicle {
public abstract void go();
}
Abstract Methods
Polymorphic Method Invocation
Vehicle v = new Car();
v.go();
So,
Vehicle v = new Vehicle(); Illegal!
Abstract Classes
Simulation Example with Abstract Vehicle Class
public class CitySimulation {
public void run() {
Vehicle [] vehicles = new Vehicle[6];
vehicles[0] = new Vehicle(); // Illegal if abstract
vehicles[1] = new Car();
vehicles[2] = new Truck();
vehicles[3] = new Truck();
vehicles[4] = new Boat();
vehicles[5] = new Airplane();
for(int i = 0; i < vehicles.length; i++) {
// Guaranteed to invoke a real (non-abstract) method
vehicles[i].go()
}
}
}
BLANK
Polymorphism Example Part 3
Interfaces
Updated Requirements
How Do We Start the Simulation Now?
Inheritance Hierarchy with Interfaces
Interfaces
Interfaces (cont.)
Interfaces (cont. 2)
Simulation Example with Interface
public class CitySimulation {
public void run() {
Moveable[] moveables = new Moveable[6];
moveables[0] = new Car();
moveables[1] = new Car();
moveables[2] = new Truck();
moveables[3] = new Truck();
moveables[4] = new Person();
moveables[5] = new Dog();
for(int i = 0; i < moveables.length; i++) {
moveables[i].go()
}
}
}
Citations (Polymorphism Example Part 3)
1. Diagrams created by course authors: Ken Rodham and Jerod Wilkerson.
Creating an Interface
Creating an Interface
public interface Moveable {
void go();
}
Extending an Interface
public interface MyInterface extends Moveable, Comparable {
void myMethod();
void myOtherMethod();
}
BLANK
Implementing an Interface
Implementing an Interface
public class Person implements Moveable {
public void go() {
// Code to make person go
}
}
Implementing an Interface with An Abstract Class in Java
public abstract class Vehicle implements Moveable {
}
Extending a Class and Implementing Multiple Interfaces
public class Employee extends Person implements Moveable, Comparable{
public void go() {
// Code to make person go
}
public int compareTo(Object obj) {
// Code to compare two employees
}
}
BLANK