Question on Inheritance & Polymorphism A) Given this code: Animal myDog = new Do
ID: 3907691 • Letter: Q
Question
Question on Inheritance & Polymorphism
A) Given this code:
Animal myDog = new Dog(“Spot”);
myDog.speak();
What do we know about the Dog class and the Animal class?
B) Given a class Animal has a child Cat and a child BigCat. Cat has a child class Tabby and Big Cat has a child class Tiger.
Given:
Animal animal;
Cat kitty = new Cat();
BigCat big = new BigCat();
Tiger tig = new Tiger();
Tabby tab = new Tabby();
Identify each line of code that will cause an error:
Error yes/no
Why?
animal = big;
kitty = null;
tab = tig;
big = tig;
big = tab;
tig = kitty;
Error yes/no
Why?
animal = big;
kitty = null;
tab = tig;
big = tig;
big = tab;
tig = kitty;
Explanation / Answer
SOLUTION:-
A):- Animal class is the super class and Dog is the sub class. Here super class Animal's reference variable myDog is using for referring the sub class Dog and extended from the super class Animal. And if the method speak() will exist in super class but overridden via subclass then it is called overriding and speak() will be executed.
B):-
==================================================================================
error: Yes/No Why? animal=big; NO Because The super class's reference variable can be utilized for referring the sub class object extended from super class kitty = null; NO NULL can be assigned to any object reference tab=tig; YES Because these is no connection between Tabby and Tiger big=tig; NO Because The super class's reference variable can be utilized for referring the sub class object extended from super class big=tab; YES Because these is no connection between BigCat and Tabby tig=kitty; YES Because these is no connection between Tiger and CatRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.