87.2k views
1 vote
QUESTIONS / TASKS • Which class is the superclass? Which is the subclass? What does it mean that the Cat class extends the Animal class? • The Cat class cannot directly access the fields it inherits from Animal. Why not? • The subclass constructor typically calls the superclass constructor to initialize the inherited fields. Write a constructor for the Cat class above. It should take as parameters the cat’s name and a boolean indicating whether it is short-haired, and it should call the superclass constructor to initialize the inherited fields. Update the test program to create an instance of class Cat. Cat c = new Cat("Kitty", false); • To manipulate the inherited fields, the subclass can use the inherited accessor and mutator methods. Write a toString method for the Cat class above. It should return a string consisting of the cat’s name followed by either " (short-haired)" or " (long-haired)". Update the test program to test your method. System.out.println( c ); • The subclass can override an inherited method, replacing it with a version that is more appropriate. Write an isSleeping method for the Cat class. It should reflect the fact that cats seem to sleep all of the time! Update the test program to test your method.

User Ssj
by
4.4k points

1 Answer

5 votes

Answer:

Check the explanation

Step-by-step explanation:

Q1 ) class Animal is the superclass, as the Cat class is inherited from Animal class

and the class Cat is the subclass.

The statement class Cat extends Animal means that, the class Cat is inherited from class Animal and class Cat has all variables and methods defined in class Animal (except private variable and methods).

Q2 ) The class Cat cannot directly access the fields it inherits from Animals, because of all the fields in class Animals are private and the extend keyword-only make the public fields accessible through the Cat class.

Q3 )

public Cat(String name, boolean isShortHaired) //constructor with 2 parameters String and boolean

{

super(name,3); //calling constructor of super class,with fields

this.isShortHaired = isShortHaired;

}

Q 4)

public String toString() //to string method

{

if(isShortHaired == true)

return super.toString+" (Short Haired) "; //returns name (Short Haired)

else

return super.toString+" (Long Haired) "; //returns name (Long Haired)

}

Q 5)

public boolean isSleeping (int hour, int minute) //overriding isSleeping method of super class

{

if (hour > 24 || hour <0 || minute > 60 || minute < 0) {

throw new IllegalArgumentException ("invalid timespecified");

}

return true; //returning true for all the valid time entered

}

User Roshan Gautam
by
3.5k points