Inheritance Lab
This is a lab assignment and so you will not be submitting it. However, the concepts and practice will help you on both the homework and exams so I encourage you to make a serious effort on it during class and consider finishing it outside of class.
Goal
Get practice writing code that uses inheritance and see how the finer details of inheritance work.
Setup
- Mount the COURSES drive
- Create a new folder in your STUWORK called
InheritanceLab
Exercise 1
We’ll first make some classes, remember to compile and run your code after each step to catch any errors early!
-
Create an
Animal
class that has the instance variableshappiness
andage
and getters/setters for these variables. (Make themInteger
s for convinience later.) -
Choose a species of animal and create a subclass of
Animal
in its own file. Remember that you create a subclass withextends
. To compile and test your subclass, you’ll need to also compileAnimal
. An easy way to do this is to typejavac *.java
, which will compile all the Java files in the directory. -
Within your subclass, make another instance variable called
happiness
. Make a getter and setter with different names from theAnimal
class getter and setter (so if you hadsetHappiness
in Animal, you could dosetHappinessDog
in a Dog subclass). -
In
main
of your subclass create an instance of your subclass to make sure everything is working.
Exercise 2
We’ll now explore what is going on with those happiness
variables.
-
In
main
, use your setters to set thehappiness
of your instance to two different values (meaning use theAnimal
setter to set to 5 and the subclass setter to set to 10, for example) -
Add
println
statements that demonstrate if the setters are accessing the same variable or different variables and write your answer on the worksheet.
Exercise 3
We’ll now explore how methods in subclasses interact with methods in superclasses.
-
Create a method
printStatus
in your subclass that prints something to the terminal. -
In
main
create two instances of your subclass but declare one as typeAnimal
and one as your subclass type:
Animal test = new Dog();
Dog test2 = new Dog();
-
Try to call
printStatus
on both objects. One will report an error. Record which one and the error on the worksheet. -
Create a method
printStatus
inAnimal
that prints something different than the method in your subclass. -
Try again to call
printStatus
on the objects you created in (b). Record on your worksheet which method is accessed for each object. -
On your worksheet, reflect on why you think you got the errors that you did and what that tells you about what Java is doing when it tries to compile and when it runs a program. (Don’t worry about being right, we’ll talk about it!)
Exercise 4
Let’s think some more about overriding methods in a subclass.
-
In your subclass method
printStatus
, usesuper
to call theprintStatus
method ofAnimal
. -
In your subclass, add a new method
pet
using the@Override
annotation:
@Override
public void pet() {
System.out.println("Petting the animal!");
}
-
Report the error that you see on your worksheet.
-
Correct the error by adding
pet
to theAnimal
class as well and verify the error is resolved. -
Reflect on your worksheet on whether you find the
@Override
annotation useful and if you plan to use it (there is no right or wrong answer, I just am curious what you think!).
Exercise 5
If you didn’t already, go back and add JavaDocs method documentation for each of your methods.