A4: Types and Interfaces

CPSC 210

Learning Goals

  • How to specify a new type as Interface or Class
  • How to determine which method can be run
    • ...and which is run
  • How a Class implements an Interface
  • How type substitution works in variable assignment and parameter passing (hint: the same in both)

Java Interfaces

  • Specify new type of data without method implementations
  • Implementation is in class that implements the interface

implements

super type

subtype

subtype

must provide implementation for all methods in List

can have additional methods

List<E>

<<interface>>

ArrayList<E>

LinkedList<E>

{

Java Interfaces (2)

  • Cannot be instantiated themselves, but can be assigned instances of its subtypes
List<Integer> myList = new ArrayList<>();
List<Integer> myOtherList = new LinkedList<>();

Apparent Type

Actual Type

  • Apparent type determines what methods can be called on an object
    • e.g. can only call methods on myList and myOtherList that are specified in the List interface

Why Interfaces?

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"

Why Interfaces? (2)

void makeThingBark(Barking barker) {
  System.out.println(barker.bark());
}

Polymorphism

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);
  }
}

Polymorphism (2)

void fetchStick()

Dog

Coyote

void stealHumanFood()

Fox

void fetchChicken()

Wolf

boolean isEndangered()

Barking

void bark()

<<interface>>

Lecture Ticket Review

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
  }
}

Lecture Ticket Review (2)

Lecture Ticket Review (3)

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
  }
}

Lecture Lab: Learners

Lecture Lab

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

Lecture Lab (2)

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:

  1. What is the apparent type for person
  2. What is the actual type for person?
  3. Draw the type hierarchy
  4. Why is "person" showing up as not compiling in the call to school.serveCoffee?
  5. Why is the call to learner.drinkCoffee not compiling?

A4: Types and Interfaces

The End - Thank You!

Extra: Coverage Testing

CPSC210 - A4 Types and Interfaces

By meghanallen

CPSC210 - A4 Types and Interfaces

  • 138