fork download
  1. interface Talker
  2. {
  3. String talks();
  4. }
  5.  
  6. class Pet<T extends Talker>
  7. {
  8. private T pet;
  9.  
  10. public Pet(T pet)
  11. {
  12. this.pet = pet;
  13. }
  14.  
  15. public String talks()
  16. {
  17. return pet.talks();
  18. }
  19. }
  20.  
  21. class Cat implements Talker
  22. {
  23. public String talks()
  24. {
  25. return "Meow";
  26. }
  27. }
  28.  
  29. class Dog implements Talker
  30. {
  31. public String talks()
  32. {
  33. return "Woof";
  34. }
  35. }
  36.  
  37. public class Main
  38. {
  39. public static void main(String[] args)
  40. {
  41. Pet<Cat> cat = new Pet<Cat>(new Cat());
  42. System.out.println("Cat says " + cat.talks());
  43.  
  44. Pet<Dog> dog = new Pet<Dog>(new Dog());
  45. System.out.printf("Dog says " + dog.talks());
  46. }
  47.  
  48. }
Success #stdin #stdout 0.03s 245632KB
stdin
Standard input is empty
stdout
Cat says Meow
Dog says Woof