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.
// 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment