Created
April 3, 2015 20:13
-
-
Save ecornell/83018875ed1abd71f8fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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