Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Cat can bark

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);
    }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.