import java.util.*; public class MainClass { static interface Animal { void bark(); } static class Dog implements Animal { private String name; public Dog(String name) { this.name = name; } public void bark() { System.out.println("<" + name + "> Wouaf!"); } } static class Cat implements Animal { private String name; public Cat(String name) { this.name = name; } public void bark() { System.out.println("<" + name + "> Meow!"); } } public static void main(String[] args) { List<Dog> dogs = new ArrayList<Dog>(); addTo(dogs, new Dog("Medor")); addTo(dogs, new Cat("Persy")); // uh!? addTo(dogs, new Dog("Rex")); System.out.println("C'mon dogs, let's bark:"); for (Animal animal : dogs) { animal.bark(); } } private static <T> void addTo(List list, T animal) { list.add(animal); } }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more