19 The following Java code presents three class definitions and four object inst
ID: 3725458 • Letter: 1
Question
19 The following Java code presents three class definitions and four object instantiations (below the line). What is displayed by the four printin statements at the end? public class Vertebrate public String whoAreYou0 ( return "Vertebrate",) public class Mammal extends Vertebrate public String whoAreYou0 ( return "Mammal", ) public class Canine extends Mammal public String whoAreYou0 ( return "Canine" ) Vertebrate v0 = new Vertebrate(); Vertebrate v1- new Mammal); Mammal m - new Canine); Vertebrate v2 new Canine(); System.out.printin (v0.whoAreYou)) System.out.println (v1.whoAreYou)), System.out.println(m.whoAreYou) System.out.println(v2.whoAreYou)); O Vertebrate, Vertebrate, Mammal, Vertebrate O Vertebrate, Mammal, Canine, Vertebrate O Vertebrate, Mammal, Canine, CanineExplanation / Answer
Answer : (a)
v0 executes the whoAreYou() function in Vertebrate class. So, it prints
Vertebrate
A reference of super class can also refer to the object of the base class, but it can only access the functions in the super class. In case of overridden function, the function in the super class is executed.
So, v1 points the object of type Mammal, but the function is called in the Vertebrate class. So, it prints
Vertebrate
Also, m points the object of type Canine, but the function is called in the Mammal class. So, it prints
Mammal
So, v2 points the object of type Vertebrate, but the function is called in the Vertebrate class. So, it prints
Vertebrate
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.