105k views
1 vote
Given two classes, Animal and Mammal, which of the following situations would make the statement valid?

Animal a = new Mammal("Elephant");
A) Mammal extends Animal and Mammal has a constructor with one parameter of the String type.
B) Mammal extends Animal and Animal has a constructor with one parameter of the String type.
C) Animal has a method Mammal that takes one parameter of the String type.
D) Animal has a public data member String Mammal
E) None of the above

1 Answer

5 votes

Final answer:

The statement 'Animal a = new Mammal("Elephant");' is valid when the Mammal class extends the Animal class and includes a constructor that accepts one String parameter.

Step-by-step explanation:

The correct answer to the question "Given two classes, Animal and Mammal, which of the following situations would make the statement Animal a = new Mammal("Elephant") valid?" is A) Mammal extends Animal and Mammal has a constructor with one parameter of the String type. This situation implies that the class Mammal is a subclass of Animal, and can therefore be referred to by a variable of type Animal. Furthermore, the constructor of Mammal accepts one String parameter which in this case is "Elephant".

Such object-oriented principles are a cornerstone of inheritance in programming where subclass instances can be treated as instances of their superclass.

User Anatoly E
by
7.6k points