Pets in boxes

Report a typo

There are animals hiding in boxes.

Finish the showAnimal method that accepts an instance of Box and writes to the console the name of the animal from the box.

It is important that the box has to be one that can only contain animals (objects of class Animal or its subclasses) and not any other objects.

Write a program in Java 17
class BoxInside {

// Complete this method
public static void showAnimal...
}

// Don't change the code below
class Animal {

private String name;

public Animal(String name) {
this.name = name;
}

public String toString() {
return name;
}
}

class Box<T> {

private T animal;

void setAnimal(T animal) {
this.animal = animal;
}

T getAnimal() {
return animal;
}
}
___

Create a free account to access the full topic