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 - Create a document Answers.txt where you’ll write your answers to some questions below
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
Animalclass that has the instance variableshappinessandageand getters/setters for these variables. (These can beIntegers orStrings depending on what you think they should contain.) -
Choose a species of animal and create a subclass of
Animalin 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 theAnimalclass getter and setter (so if you hadsetHappinessin Animal, you could dosetHappinessDogin a Dog subclass). -
In
mainof 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 thehappinessof your instance to two different values (meaning use theAnimalsetter to set to 5 and the subclass setter to set to 10, for example) -
Add
printlnstatements that demonstrate if the setters are accessing the same variable or different variables and write your answer in Answers.txt.
Exercise 3
We’ll now explore how methods in subclasses interact with methods in superclasses.
-
Create a method
printStatusin your subclass that prints something to the terminal. -
In
maincreate two instances of your subclass but declare one as typeAnimaland one as your subclass type:
Animal test = new Dog();
Dog test2 = new Dog();
-
Try to call
printStatuson both objects. One will report an error. Record which one and the error in Answers.txt. -
Create a method
printStatusinAnimalthat prints something different than the method in your subclass. -
Try again to call
printStatuson the objects you created in (b). Record in Answers.txt which method is accessed for each object. -
In Answers.txt, 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, usesuperto call theprintStatusmethod ofAnimal. In Answers.txt, come up with an example of when overriding a superclass method and then calling the superclass method could be useful. -
In your subclass, add a new method
petusing the@Overrideannotation:
@Override
public void pet() {
System.out.println("Petting the animal!");
}
-
Report the error that you see in Answers.txt.
-
Correct the error by adding
petto theAnimalclass as well and verify the error is resolved. -
Reflect in Answers.txt on whether you find the
@Overrideannotation 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.
Extra
If you finish early, work on the exercises linked through Moodle. Some of them go beyond the Java syntax that we’re focused on, but feel free to refer to the included readings to learn more!