Zoo Abstract Inheritance Lab Objective: Students should be able to declare an abstract class. Students should be able to create more subclasses for the class.
Zoo Abstract Inheritance Lab
Objective: Students should be able to declare an abstract class. Students should be able to create more subclasses for the class. They should be able to create an array of class objects. They should be able to write a tester program to display all the functionality of their program.
Its good programming style to keep the amount of code to a minimum and to reuse existing code as much as possible. Code that is economical is easy to maintain. The concept of inheritance does just that: it illustrates a way to make code both economical and extendable.
When creating a model using inheritance, you start by defining a more general class with common instance variables and methods. The more specific classes are told to extend the general class. The general class then becomes the superclass and the others become the subclasses. The subclasses automatically inherit the instance variables and methods from the superclass.
The result is called the inheritance hierarchy; a simple such hierarchy is shown here. In this case, the Animal class is the superclass. It has two subclasses, Cow and Camel. (This doesnt work in reverse. A subclass can extend only one class.)
Lets model this animal hierarchy.
Write an Animal Class. This will be the superclass.
This class should be declared as abstract because we dont want to be able to create an instance of this class.
This class will have 2 attributes: a name and a sound.
Write a constructor that takes 2 parameters: name and sound.
Write accessor methods only for each attribute.
Write a toString method that prints out the attributes in the form: Spot says woof.
Write a class called Cow. A cow is an animal.
This class will not have any additional instance variables.
Define a constructor that has a name and sound as a parameter.
All methods will be inherited. You do not need to write any additional methods.
Write a class called Camel.
A camel will add an instance variable for the number of humps.
Provide a constructor that accepts 3 parameters: name, sound, and number of humps.
Write an accessor method to get the number of humps.
Override the toString method to display a message in the format: The camel bertha says spit and has 2 humps
Define your own subclass. Your subclass should be unique so be creative.
Write a class called ZooTester.java. Create an array of 5 animals and print out each animal. Be sure to test any additional methods you write.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started