Create an inheritance hierarchy of Rodent: Mouse, Gerbil, Hamster, etc. In the base class, provide methods that are common to all Rodents, and override these in the derived classes to perform different behaviors depending on the specific type of Rodent. Create an array of Rodent, fill it with different specific types of Rodents, and call your base-class methods to see what happens.
(a) Create an inheritance hierarchy of Rodent: Mouse, Gerbil, Hamster, etc. In the base class, provide
methods that are common to all Rodents, and override these in the derived classes to perform different
behaviors depending on the specific type of Rodent. Create an array of Rodent, fill it with different specific types of Rodents, and call your base-class methods to see what happens.
(b) Create a Cycle class, with subclasses Unicycle, Bicycle and Tricycle. Demonstrate that an instance of each
type can be upcast to Cycle via a ride( ) method. Using the above Cycle hierarchy, add a balance( ) method
to Unicycle and Bicycle, but not to Tricycle. Create instances of all three types and upcast them to an array
of Cycle. Try to call balance( ) on each element of the array and observe the results. Downcast and call
balance( ) and observe what happens.
Trending now
This is a popular solution!
Step by step
Solved in 7 steps with 1 images