/home/wpollock1/public_html/Java/Carnivore.java

// Carnivore.java shows (in a somewhat gruesome way) why polymorphism
// is useful.  In the for loop (at the bottom), without polymorphism
// what version of favoritePrey() should get invoked?
//
// Written 1/2019 by Wayne Pollock, Tampa Florida USA

import java.util.*;

abstract public class Carnivore {
    String name;
    abstract public String favoritePrey ();
    public Carnivore ( String name ) {
        this.name = name;
    }
    // ...
}

class Lion extends Carnivore {
    Lion ( String name ) {
        super(name);
    }

    public String favoritePrey () {
        // ...
        return "water buffalo";
    }
}

class Tiger extends Carnivore {
    Tiger ( String name ) {
        super(name);
    }

    public String favoritePrey () {
        // ...
        return "deer";
    }
}

class CarnivoreDemo {
    // ...
    public static void main ( String [] args ) {
        // ...
        List<Carnivore> animals = new ArrayList<>();
        animals.add( new Lion("Leo") );
        animals.add( new Tiger("Tony") );
        showInfo (animals);
    }

    public static void showInfo( List<Carnivore> animals ) {
        // Polymorphism at work:
        for ( Carnivore c : animals ) {
            System.out.print( "Name: " + c.name );
            System.out.println( ", Eats: " + c.favoritePrey() );
        }
    }
}