Felix Grund
Instructor @ UBC
I want to make any barking animal bark and I don't care if it's a dog or not!
for (Barking barker : barkers) {
barker.bark();
}
// "WOOOFF"
// "WUUUUUFFF"
// "WAAAAAFFF"
// "GRAAAA"
// "WOOOFF"
I want to make any barking animal bark and I don't care if it's a dog or not!
But dogs are not only bakers. They are also walkers...
void walk();
Dog
void fetchStick()
Fox
void fetchChicken()
Walking
void walk()
<<interface>>
Barking
void bark()
<<interface>>
void makeThingBark(Barking barker) {
barker.bark();
}
void makeThingWalk(Walking walker) {
walker.walk();
}
Barking barkingWolf = new Wolf();
Walking walkingWolf = new Wolf();
Wolf explicitWolf = new Wolf();
makeThingBark(barkingWolf); // ???
makeThingWalk(barkingWolf); // ???
makeThingBark(walkingWolf); // ???
makeThingWalk(walkingWolf); // ???
makeThingBark(explicitWolf); // ???
makeThingWalk(explicitWolf); // ???
Bakery
void serveCookie()
<<interface>>
CoffeeShop
void serveCookie()
void serveCoffee()
<<interface>>
Kiosk
what method/s
must be implemented
in this class?
Bakery
void serveCookie()
<<interface>>
CoffeeShop
void serveCookie()
void serveCoffee()
<<interface>>
Kiosk
what method/s
must be implemented
in this class?
Bakery
void serveCookie()
<<interface>>
CoffeeShop
void serveCookie()
void serveCoffee()
<<interface>>
Kiosk
what method/s
must be implemented
in this class?
Which would compile?
Learner student = new Student();
student.getCourses();
Student student = new Student();
student.getCourses();
HasCourses somebodyWithCourses = new Student();
somebodyWithCourses.getCourses();
B
A
C
University uni = new University();
Learner learner = new Student();
uni.teach(learner);
University uni = new University();
HasCourses somebodyWithCourses = new Student();
uni.teach(somebodyWithCourses);
University uni = new University();
Student student = new Student();
uni.teach(student);
A
B
C
public class University {
public void teach(
Learner learner) {
// ...
}
public void register(
HasCourses somebodyWithCourses) {
// ...
}
}
Which would compile?
University uni = new University();
Learner learner = new Student();
uni.register(learner);
D
University uni = new University();
Student student = new Student();
uni.register(student);
E
University uni = new University();
HasCourses somebodyWithCourses
= new Student();
uni.register(somebodyWithCourses);
F
By Felix Grund