- inheritance
- subtyping
- substitution
- polymorphism

Alberto Ferrari
Ingegneria dell'Informazione, UniPR





public class Post {
private String username; // username of the post’s author
private long timestamp;
private int likes;
private ArrayList<String> comments;
// Constructors and methods omitted.
}
public class MessagePost extends Post {
private String message;
// Constructors and methods omitted.
}
public class PhotoPost extends Post {
private String filename; private String caption;
// Constructors and methods omitted.
}
public class MessagePost extends Post {
private String message; // an arbitrarily long, multi-line message
/**
* Constructor for objects of class MessagePost.
* @param author The username of the author of this post.
* @param text The text of this post.
*/
public MessagePost(String author, String text) {
super(author);
message = text;
}
// Methods omitted.
}

Vehicle v1 = new Vehicle();
Vehicle v2 = new Car();
Vehicle v3 = new Bicycle();
Car c1 = new Vehicle(); // this is an error!
Vehicle v;
Car c = new Car();
v = c; // correct
c = v; // error
c = (Car) v; // okay
Vehicle v;
Car c;
Bicycle b;
c = new Car();
v = c; // okay
b = (Bicycle) c; // compile time error!
b = (Bicycle) v; // runtime error!

ArrayList<Integer> myList;
...
int val = 3;
myList.add(val);
int myVal;
myVal = myList.remove(0);
Alberto Ferrari
Ingegneria dell'Informazione, UniPR
www.ce.unipr.it/~aferrari/