Explore a diverse range of topics and get expert answers on IDNLearn.com. Find the information you need quickly and easily with our reliable and thorough Q&A platform.

A bear is an animal and a zoo contains many animals, including bears. Three classes Animal, Bear, and Zoo are declared to represent animal, bear and zoo objects. Which of the following is the most appropriate set of declarations?
a) public class Animal extends Bear
{
...
}
public class Zoo
{
private Animal[] myAnimals;
...
}
b) public class Animal extends Zoo
{
private Bear myBear;
...
}
c) public class Bear extends Animal, Zoo
{
...
}
d) public class Bear extends Animal implements Zoo
{
...
}
e) public class Bear extends Animal
{
...
}
public class Zoo
{
private Animal[] myAnimals;
...
}


Sagot :

Answer:

a) public class Bear  extends Animal{ ... }

public class Zoo {  private Animal[] myAnimals;

... }

Explanation:

The question is an illustration of inheritance in Java programming language.

The syntax to follow is:

class subclass extends superclass {...}

From the question:

Bear [tex]\to[/tex] subclass

Animal [tex]\to[/tex] superclass

So, the following declaration will be used:

class Bear  extends Animal{ ... }

Also from the question, the zoo object represents collection of many animals.

So, its declaration will follow:

class class-name{ ..... }

i.e.

class zoo{ .... }

The ... can then be replaced with declaration of each animal or an array to represent the declarations..

Hence, (e) is correct

The most appropriate set of declarations is; Option E

What is the best programming pattern?

From the parameters given in the question, we can say that;

Bear is a subclass

Animal is a superclass

This means that the declaration that will be used is;

public class Bear  extends Animal{ ... }

Now, we also see that Zoo are declared to represent animal, bear and zoo objects. Thus, the declaration would be;

public class Zoo

{

private Animal[] myAnimals;

...

}

Looking at the given options, only option E is correct.

Read more about Java Programming at; https://brainly.com/question/18554491