2

I have researched the topic further and i realise that for an example to purely detail parametric polymorphism, it must not utilise "implements" (which would detail subtype polymorphism) but instead should utilise generics .

This is my previous question: What is parametric polymorphism in Java (with example)?

This is the reworked code to use generics with a single issue.

class Pet<T>
{
  private T pet;

  public Pet(T pet)
  {
    this.pet = pet;
  }

  public String talks()
  {
    // what do i write here?
    // pet.talks(); is undefined for type T
  }
}

class Cat
{
  public String talks()
  {
    return "Meow";
  }
}

class Dog
{
  public String talks()
  {
    return "Woof";
  }
}

public class TestPet
{

  public static void main(String[] args)
  {
    Pet<Cat> cat = new Pet<Cat>(new Cat());
    System.out.println("Cat says " + cat.talks());

    Pet<Dog> dog = new Pet<Dog>(new Dog());
    System.out.println("Dog says " + dog.talks());
  }

}

There is a single issue with my code detailed in comments. I'm unsure how to write it without using the implements command. Do you know what to write?

Community
  • 1
  • 1
Danny Rancher
  • 1,923
  • 3
  • 24
  • 43
  • [Parametric polymorphism in Java](http://www.javaworld.com/javaworld/jw-02-2000/jw-02-jsr.html) – Jeffrey Apr 17 '12 at 03:05
  • Is this for an assignment? Since your return type for 'talks()' is String, the use of generics to represent a pet is kind of pointless. I would be better off to just do 'abstract class Pet implements Talkable,' or something of the sort. – Martin Tuskevicius Apr 17 '12 at 03:20
  • @MartinTuskevicius yes it is, but i need to use purely parametric_polymorphism/generics in a java example. – Danny Rancher Apr 17 '12 at 13:28

1 Answers1

8

You need to add an upper bound to T such that it implements an interface which guarantees that there is a talks() method in the type T.

class Pet<T extends Talker>
{
    private T pet;

    public Pet(T pet)
    {
        this.pet = pet;
    }

    public String talks()
    {
        return pet.talks(); // is now defined for type T
    }
}

interface Talker
{
    String talks();
}

Demo, thanks to Ray Toal: http://ideone.com/yiAqM

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 1
    Beat me to it. +1 of course, but: Pedantic: must say `return pet.talks()` http://ideone.com/30ATh – Ray Toal Apr 17 '12 at 03:08
  • @RayToal not pedantry, but compile-error fixing. Still, I beat you to that, too :P You did, however, beat me to the ideone link, so I'm going to poach that if you don't mind. – Matt Ball Apr 17 '12 at 03:09
  • wow! thank you! although your suggestion works, when using an interface i find myself having to use "implements" on the Cat and Dog classes. Am i doing it wrong? – Danny Rancher Apr 17 '12 at 03:17
  • 1
    @DannyRancher nope, not at all! That's exactly right. A `class implements` an `interface`; a `class extends` an `abstract class`, and (just to make things fun) an `interface extends` another `interface`. See [When to use extends and when to use interface?](http://stackoverflow.com/questions/1518780/when-to-use-extends-and-when-to-use-interface) and/or [Java Generics - implements and extends](http://stackoverflow.com/q/3985225/139010) and especially [Java generics - why is “extends T” allowed but not “implements T”?](http://stackoverflow.com/q/976441/139010). – Matt Ball Apr 17 '12 at 03:24