edubkendo
12/5/2013 - 8:18 PM

Ducktyping.java

public class DuckTyping {

    public static void duckTyping(Duck duck) {
        duck.quack();
    }
}

public class Duck {
    public void quack() {
        System.out.println("Quack!");
    }
}

public class Chicken extends Duck {

    @Override
    public void quack() {
        System.out.println("Um, cluck, cluck!");
    }
}

public class Main {

    public static void main(String[] args) {
        Duck myDuck = new Duck();
        DuckTyping.duckTyping(myDuck);

        Chicken myChicken = new Chicken();
        DuckTyping.duckTyping(myChicken);
    }

}

// =>   Quack!
// =>   Um, cluck, cluck!