Quiz: Possibly two correct answers


<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

One question asks: What line of Coode is correct when
initializing an Instance of the Object Dog.

One option is Animal spot = new Dog();

I don’t want to cherrypick, but since Dog is a Subtyp of Animal it is formally
correct and possible to store it in a Variable of the super class.

It’s not relevant at this point of the course, but that mistake should be avoided as soon as possible :stuck_out_tongue:

(I’m german, so please excuse possible spelling / term mixups)

<do not remove the three backticks above>
1 Like

This is the concept of POLYMORPHISM


Yes, @linkev , you are right. Either of these statements would create an instance of the Dog class, and assign it to the variable, spot:

Animal spot = new Dog();
Dog spot = new Dog();

In both cases, the variable, spot, refers to an instance of Dog, as you could prove by calling a method of the Dog class from that variable. For example, if the Animal class and the Dog class each have their own eat method that takes a String as an argument, then the eat method of the Dog class, rather than that of the Animal class, would execute in the following code …

Animal spot = new Dog();

It is an example of polymorphism, as @rydan says.

1 Like

yes it is polymoporphism, but it does not change a fact that you do not accept a correct answer. The question is badly formed and two answers should be accepted. otherwise you just prove of lack of knowledge in codeacademy.com fix it.