Skip to content

Instantly share code, notes, and snippets.

@ecornell
Created April 3, 2015 20:13
Show Gist options
  • Save ecornell/83018875ed1abd71f8fc to your computer and use it in GitHub Desktop.
Save ecornell/83018875ed1abd71f8fc to your computer and use it in GitHub Desktop.

Revisions

  1. ecornell created this gist Apr 3, 2015.
    65 changes: 65 additions & 0 deletions AnimalArray.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    // Animal.java

    public class Animal {
    protected String type;

    public Animal() {
    type = "Unknown";
    }

    public String getType() {
    return type;
    }
    }


    // Cat.java

    public class Cat extends Animal {

    public Cat() {
    type = "Cat";
    }

    }

    // Dog.java

    public class Dog extends Animal {

    public Dog() {
    type = "Dog";
    }

    }


    // AnimalArray.java

    import java.util.ArrayList;

    public class AnimalArray {

    public static void main(String[] args) {

    Cat cat = new Cat();
    Dog dog = new Dog();

    ArrayList<Animal> animalList = new ArrayList<Animal>();

    animalList.add(cat);
    animalList.add(dog);

    for (Animal animal : animalList) {
    System.out.println( animal.getType() );
    }
    }
    }


    // Output
    //
    // Cat
    // Dog