Polymorphism with Interfaces

Using an interface as a variable's type lets you treat an object as just one of its roles, such as treating a Teenager as a Student or a team as a list of Athletes.

Freelancer · Java · OOP · Updated

By

Interfaces allow us to define the behaviors that we want to focus in on. In the example of a high school teenager we would create the interfaces and class as follows.

Interfaces and Teenager class

Notice that the interfaces only define methods or behaviors that we would expect from that role.

The teenager implements all of the interfaces and must provide method implementations. This means that a Teenager is a Student, Musician and Athlete. So Chloe, our teenager, can do ALL of those things.

In the following code we treat Chloe as a student.

Student chloe = new Teenager();

Because our variable type is Student, this means that we can only treat Chloe as a student. Essentially, because we see her as a student, we do not know that Chloe has the ability to sing or play volleyball. So the only things we can ask her to do are to study() or takeTest().

chloe.study();
chloe.takeTest();

// this is not allowed and will not compile
chloe.practice();

Chloe still has the ability to practice, but we only know that she is a Student, and we cannot guarantee that all students have that ability.

This is the essence of abstraction. When we treat Chloe as a Student, we abstract away all of the other things that Chloe can do, and focus just on what a Student can do.

Similarly, when the entire volleyball team steps on the floor, the coach does not need to know who plays an instrument, or who understands chemistry. She can treat every team member as an athlete.

List<Athlete> team = new ArrayList<>();
team.add(new Teenager());
team.add(new Teenager());
team.add(new Teenager());
team.add(new Teenager());

team.forEach(player -> player.train());

Do it: warm up the volleyball team

Before each game, the coach calls out the warm-up, and every player on the floor does their own drill. The coach doesn't need to know who studies math or who sings: she treats every player as an Athlete. Today the team includes Chloe, a Teenager, and the assistant coach, Ms. Park, who warms up with them.

Given at the bottom of the file:

  • the interface Student, with String study(), and the interface Athlete, with String train();
  • the class Teenager, which implements Student so far: study() returns the name, then " studies math";
  • the class Coach, which implements Athlete: train() returns the name, then " runs drills".

The starter doesn't compile yet: a test puts a Teenager on the team, and a Teenager isn't an Athlete until you make it one.

The method gets one value:

  • team: the players on the floor, a List<Athlete>, in the order the coach calls them.

For example:

List<Athlete> team = List.of(new Teenager("Chloe"), new Coach("Ms. Park"));
Player Class train() returns
Chloe Teenager "Chloe practices serves"
Ms. Park Coach "Ms. Park runs drills"

So warmUp(team) returns a list of those two lines, in that order.

Your job: make every teenager an athlete, then write the warm-up.

  • Make Teenager implement Athlete too, as well as Student.
  • A teenager's train() returns the name, then " practices serves".
  • Write the method warmUp(List<Athlete> team) so it returns a List<String>: what each player's train() returns, in the team's order.
  • An empty team gives an empty list.
  • Leave study(), Coach and the two interfaces as they are.

Practiced live on the lesson page: write the Java right there and check it.

Cheat sheets