The exercises in this project are designed to help you understand inheritance in Java,
To get started, open the project file located in inheritance-in-java/pom.xml
in IntelliJ.
This project contains a command-line application for interacting with animals in a zoo. This is the first project where you will be required to create new classes in the project.
Because some classes are missing, you will need to uncomment a section in the Main
class and
in the ExerciseTests
class to integrate the new class into the application and associated tests.
The Main
class contains the main method that runs the application. Other than the uncommenting
mentioned above, you will not need to modify this class at all, though you are, as always,
encouraged to look through it to understand how the application works.
The Animal
class contains the code for an abstract animal. You will not need to modify this class,
but you should look at the code to understand how an abstract class is defined and how it is used.
This exercise will involve creating new classes that extend the Animal
class.
To complete this exercise, you will need to create three new classes that extend the Animal
class.
Each of these classes, will require creating a new file in the src/main/java/org.example/src
directory
and implementing a class derived from the Animal
class.
The Elephant
class will represent an elephant. To create this class, you'll need to:
- Create a new class in the
src/main/java/org.example/src
directory calledElephant
- Have the
Elephant
class extend theAnimal
class - Add a constructor that takes a
name
parameter and calls the constructor of theAnimal
class. If you hover your mouse over the red-underlined class declaration, you will see a lightbulb that will allow you to automatically add this constructor. - Implement the abstract
speak
method to return a String made up ofname + " makes a trumpet sound."
Notice that this method makes use of thename
instance variable that is defined in the parentAnimal
class. - Uncomment the
elephant
section in theMain
class to add an elephant to the zoo. - Uncomment the
elephant
section in theExerciseTests
class to add tests for theElephant
class. - Run the application and go through all of the options to see that the two defined elephants are properly behaving as you indicated.
- Run the tests to verify that the
Elephant
class is implemented correctly.
- Create a new class in the
src/main/java/org.example/src
directory calledLion
- Have the
Lion
class extend theAnimal
class - Add a constructor that takes a
name
parameter and calls the constructor of theAnimal
class. If you hover your mouse over the red-underlined class declaration, you will see a lightbulb that will allow you to automatically add this constructor. - Implement the abstract
speak
method to return a String made up ofname + " lets out a loud ROAR!"
- Override the
sleep
method to return the result of calling thesuper.sleep
method and appending" " + name + " dreams of a delicious steak."
to the end of the result. - Uncomment the
lion
section in theMain
class to add an lion to the zoo. - Uncomment the
lion
section in theExerciseTests
class to add tests for theLion
class. - Run the application and go through all of the options to see that the two defined lions are properly behaving as you indicated.
- Run the tests to verify that the
Lion
class is implemented correctly.
- Create a new class in the
src/main/java/org.example/src
directory calledParrot
- Have the
Parrot
class extend theAnimal
class - Add a constructor that takes a
name
parameter and calls the constructor of theAnimal
class. If you hover your mouse over the red-underlined class declaration, you will see a lightbulb that will allow you to automatically add this constructor. - Implement the abstract
speak
method to return a String made up ofname + " says '" + name + " wants a cracker!'"
- Override the
eat
method to returnname + " is eating a cracker."
- Add a new method called
fly
that returnsname + " is flying around the zoo."
- Uncomment the
parrot
section in theMain
class to add an parrot to the zoo. - Uncomment the
parrot
section in theExerciseTests
class to add tests for theParrot
class. - Run the application and go through all of the options to see that the two defined parrots are properly behaving as you indicated.
- Run the tests to verify that the
Parrot
class is implemented correctly.
To run the application, right-click on the src/main/java/org.example/src/Main
class and select "Run 'Main.main()'".
This will run the application and you will be able to interact with it in the console.
As always, final verification of your code should be done by running the unit tests.
To run the tests, right-click on the test/java/org.example/src/ExerciseTests
class and select "Run 'ExercisesTests'".
This will run all of the tests for the exercises. When a test succeeds, you will see a green checkmark next to the test.
When a test fails, you will see a red X next to the test. Once all of the tests pass, you have completed the exercises.