OnJava8-Examples/generics/DogsAndRobots.java
Bruce Eckel ede3954d86 March 2021 Book Update
See notes in "Foreword to the Leanpub Edition"
2021-03-04 16:15:04 -07:00

44 lines
1019 B
Java

// generics/DogsAndRobots.java
// (c)2021 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// No (direct) latent typing in Java
import reflection.pets.*;
class PerformingDog extends Dog implements Performs {
@Override
public void speak() { System.out.println("Woof!"); }
@Override
public void sit() { System.out.println("Sitting"); }
public void reproduce() {}
}
class Robot implements Performs {
@Override
public void speak() { System.out.println("Click!"); }
@Override
public void sit() { System.out.println("Clank!"); }
public void oilChange() {}
}
class Communicate {
public static <T extends Performs>
void perform(T performer) {
performer.speak();
performer.sit();
}
}
public class DogsAndRobots {
public static void main(String[] args) {
Communicate.perform(new PerformingDog());
Communicate.perform(new Robot());
}
}
/* Output:
Woof!
Sitting
Click!
Clank!
*/