/** * A very simple shelter class should one of the uses of object inheritance * @author gtowell * Created: Feb 2021 */ public class Shelter { /** * Holds all of the pets in the shelter */ private Pet[] animals = new Pet[100]; /** * The number of pets in the shelter. This could be calculated ont he fly from animals */ int animalCount = 0; /** * Add an animal to the shelter * @param animal the animal to be added */ public void addAnimal(Pet animal) { animals[animalCount++] = animal; } /** * Get a particular animal from the shelter. * The method is pretty horrific in its abuse of encapsulation * Also, does no bounds checking on its arg, so could throw an exception * @param location the location in that array to get the animal from * @return the animal (or null). */ public Pet getAnimal(int location) { return animals[location]; } public static void main(String[] args) { Shelter shelter = new Shelter(); shelter.addAnimal(new Dog()); shelter.addAnimal(new Cat()); Pet aa = shelter.getAnimal(1); System.out.println(aa); if (aa instanceof Cat) { Cat c = (Cat)aa; //Danger here System.out.println(c.toString()); }}}