implements
super type
subtype
subtype
must provide implementation for all methods in List
can have additional methods
List<E>
<<interface>>
ArrayList<E>
LinkedList<E>
{
List<Integer> myList = new ArrayList<>();
List<Integer> myOtherList = new LinkedList<>();
Apparent Type
Actual Type
Not only dogs bark!
I want to make any barking animal bark and I don't care if it's a dog!
for (Barking barker : barkers) {
barker.bark();
}
// "WOOOFF"
// "WUUUUUFFF"
// "WAAAAAFFF"
// "GRAAAA"
// "WOOOFF"
void makeThingBark(Barking barker) {
System.out.println(barker.bark());
}
Barking wolf = new Wolf();
Barking coyote = new Coyote();
Fox fox = new Fox();
makeThingBark(wolf);
makeThingBark(coyote);
makeThingBark(fox);
List<Barking> barkers;
barkers = new ArrayList<>();
barkers.add(new Wolf());
barkers.add(new Coyote());
barkers.add(new Fox());
makeAllThingsBark(barkers);
void makeDogBark(Dog dog) {
dog.bark();
}
Barking barker = new Wolf();
Barking anotherBarker = new Dog();
makeDogBark(barker);
makeDogBark(anotherBarker);
void makeAllThingsBark(List<Barking> barkers) {
for (Barking barker : barkers) {
makeThingBark(barker);
}
}
void fetchStick()
Dog
Coyote
void stealHumanFood()
Fox
void fetchChicken()
Wolf
boolean isEndangered()
Barking
void bark()
<<interface>>
public interface Flyer {
public void fly();
}
public class Plane implements Flyer {
@Override
public void fly() {
System.out.println("Cruising at 35,000 ft...");
}
}
public class Seagull implements Flyer {
@Override
public void fly() {
System.out.println("Soaring with the wind...");
}
}
public class Launcher {
public void launch(Flyer flyer){
System.out.println("Go fly, oh flyer!");
flyer.fly();
}
public void refuel(Plane plane){
System.out.println("Refueling the plane...");
}
}
public class FlyerApp {
public static void main(String[] args) {
Launcher launcher = new Launcher();
Flyer plane = new Plane();
Flyer seagull = new Seagull();
// launcher.launch(plane); // A
// launcher.launch(seagull); // B
// launcher.refuel(plane); // C
}
}
public interface Flyer {
public void fly();
}
public class Plane implements Flyer {
@Override
public void fly() {
System.out.println("Cruising at 35,000 ft...");
}
}
public class Seagull implements Flyer {
@Override
public void fly() {
System.out.println("Soaring with the wind...");
}
}
public class Launcher {
public void launch(Flyer flyer){
System.out.println("Go fly, oh flyer!");
flyer.fly();
}
public void refuel(Plane plane){
System.out.println("Refueling the plane...");
}
}
public class FlyerApp {
public static void main(String[] args) {
Launcher launcher = new Launcher();
Flyer plane = new Plane();
Flyer seagull = new Seagull();
// launcher.launch(plane); // A
// launcher.launch(seagull); // B
// launcher.refuel(plane); // C
}
}
public interface Flyer {
public void fly();
}
public class Plane implements Flyer {
@Override
public void fly() {
System.out.println("Cruising at 35,000 ft...");
}
}
public class Seagull implements Flyer {
@Override
public void fly() {
System.out.println("Soaring with the wind...");
}
}
public class Launcher {
public void launch(Flyer flyer){
System.out.println("Go fly, oh flyer!");
flyer.fly();
}
public void refuel(Plane plane){
System.out.println("Refueling the plane...");
}
}
public class FlyerApp {
public static void main(String[] args) {
Launcher launcher = new Launcher();
Flyer plane = new Plane();
Flyer seagull = new Seagull();
// launcher.launch(plane); // A
// launcher.launch(seagull); // B
// launcher.refuel(plane); // C
}
}
Clone the A04-Learners repo:
Have a look at the interface Learner
...create a method learn
Create a class Student
...that implements Learner
...why is IntelliJ complaining?
...implement learn method with "I am learning!" print
...implement drinkCoffee method
public class School {
public static void main(String[] args) {
School school = new School();
Learner person = new Student();
school.serveCoffee(person);
//why is person not compiling?
school.teach(person);
}
public void serveCoffee(Student student){
student.drinkCoffee();
}
public void teach(Learner learner){
learner.learn();
learner.drinkCoffee();
//why can't we call this method?
}
}
Now work on: