Module 5 - Animal Lab
Objective
The goal of this assignment is to help you understand the concept of inheritance in Object-Oriented Programming. You will create a simple class hierarchy that demonstrates how inheritance works.
Requirements:
Create a base class called
Animal
with the following features:A private instance variable
name
of typeString
.A constructor that takes a
String
argument to initializename
.A method
makeSound()
that prints a generic sound like "Some generic animal sound".A method
getName()
that returns the name of the animal.
Create a subclass called
Dog
that inherits fromAnimal
:Override the
makeSound()
method to print "Woof Woof".Override the
toString()
Override the
hashCode()
Override the
equals()
Create another subclass called
Cat
that also inherits fromAnimal
:Override the
makeSound()
method to print "Meow Meow".Override the
toString()
Override the
hashCode()
Override the
equals()
In your
main
method, do the following:Create 2 instances of
Dog
;Output hash code of each
Using the equals method, check to see if the objects are identical
Create 2 instances of
Cat
;Output hash code of each
Using the equals method, check to see if the objects are identical
Create an ArrayList (create 2 ArrayLists) of
Dog
andCat
objects and populate with theDog
andCat
objects you created ealier. Loop through the ArrayLists and output each animal's name, and sound using thetoString()
method.
Deliverables
Upload the following files in a zip folder that you have created:
Java class files
Screenshot of the Console with the code executing
COSC-1437 / ITSE-2457 Computer Science Dept. - Author: Dr. Kevin Roark