Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In java... In this exercise you'll demonstrate how to use inheritance and interfaces. Animal First implement an abstract class called Animal. The class should have
In java...
In this exercise you'll demonstrate how to use inheritance and interfaces.
Animal
First implement an abstract class called Animal. The class should have a constructor that takes the animal's name as a parameter. The Animal class also has nonparameterized methods eat and sleep that return nothing void and a nonparameterized method getName that returns the name of the animal.
The sleep method should print name sleeps", and the eat method should print name eats". Here name is the name of the animal in question.
Dog
Implement a class called Dog that inherits from Animal. Dog should have a parameterized constructor that can be used to name it The class should also have a nonparameterized constructor, which gives the dog the name "Dog". Another method that Dog must have is the nonparameterized bark, and it should not return any value void Like all animals, Dog needs to have the methods eat and sleep.
Below is an example of how the class Dog is expected to work.
Dog dog new Dog;
dog.bark;
dog.eat;
Dog fido new DogFido;
fido.bark;
Dog barks Dog eats Fido barks
Cat
Next to implement is the class Cat, that also inherits from the Animal class. Cat should have two constructors: one with a parameter, used to name the cat according to the parameter, and one without parameters, in which case the name is simply "Cat". Another method for Cat is a nonparameterized method called purr that returns no value void Cats should be able to eat and sleep like in the first part.
Here's an example of how the class Cat is expected to function:
Cat cat new Cat;
cat.purr;
cat.eat;
Cat garfield new CatGarfield;
garfield.purr;
Cat purrs Cat eats Garfield purrs
NoiseCapable
Finally, create an interface called NoiseCapable. It should define a nonparameterized method makeNoise that returns no value void Implement the interface in the classes Dog and Cat. The interface should take use of the bark and purr methods you've defined earlier.
Below is an example of the expected functionality.
NoiseCapable dog new Dog;
dog.makeNoise;
NoiseCapable cat new CatGarfield;
cat.makeNoise;
Cat c Cat cat;
cpurr;
Dog barks Garfield purrs Garfield purrs
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
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