Working with Classes

Starting a Dog Park

Open Dog.java and take a look at the code with a partner. Discuss what the code does.

Open Park.java. Add code to the main method that does the following:

  • Creates an array of two different dogs
  • Uses a for loop to loop through the array and make both dogs speak
  • Uses a for loop to loop through the array and make both dogs play

Compile and run your code to make sure it works.

Defining Animals

Now we want to add other animals to our Park, but we want to be able to treat them the same way we treat the Dog objects and keep them in the same array and call their methods using the same for loops. By using an abstract class, we should be able to do this without changing much of the Park code we just wrote.

Open Animal.java. As you can see by the class declaration at the top, this is an abstract class. This means we will never actually instantiate objects of this class; instead, any class that inherits from this class will need to contain the methods we declare in this class. Our code can be a mix of actual code and methods we declare as abstract methods and leave to our child classes to actually write the code for.

To make all Animals share some of the abilities of Dogs, we are going to declare two abstract methods in Animal.java: public abstract void speak(), and public abstract void play().

The constructors for all Animals will be the same as the Dog constructor, so we can create a constructor in Animal.java and then call it in our other animals. In the Animal abstract class, create an instance variable name, and a constructor that takes in a String and saves it to name (just like in Dog.java)

Update the Dog class to extend Animal. Now, instead of having our own constructor in Dog, will will simply call the Animal class’s constructor. Change Dog’s constructor to only contain the line

super(name);

You can also delete the name instance variable in Dog.

Change the array in Park to be of type Animal instead of type Dog. Compile and run your code to make sure everything still works.

The name for each animal does not need to be a user input and can be written in when you call the constructor for each animal.

Creating Squirrels and Cats

Now let’s create some more Animals! Create a Squirrel class and a Cat class that extend the Animal class in the provided Squirrel.java and Cat.java empty files. Create appropriate constructors that match what we did for Dog above, as well as speak and play methods for both classes.

Now increase your array of Animals in the Park class to size 4, and add a new Squirrel and Cat to spots 2 and 3 in the array. Compile and run your Park class - if you’ve added things appropriately, your Squirrel and Cat should speak and play without you changing any of the code in the for loops. For example, when I run my code, I get the following:

Rover says woof woof.
Spot says woof woof.
Fluffy says meow.
Yeobie says chitter chitter.
Rover chases the stick.
Spot chases the stick.
Fluffy watches the birds.
Yeobie scampers.

Use the git commands add, commit and push to save your changes to each of the Java files before you move on to the next part of the warmup.