10.5 Exercises
Factual
Recall that the maxDog method has the following signature:
public static Dog maxDog(Dog d1, Dog d2) { … }What is the static type of
Dog.maxDog(dogC, dogD)?
ShowDog dogC = new ShowDog("Franklin", "Malamute", 180, 6);
ShowDog dogD = new ShowDog("Gargamel", "Corgi", 44, 12);
Dog.maxDog(dogC, dogD);Which (if any), will compile:
Dog md = Dog.maxDog(dogC, dogD);
ShowDog msd = Dog.maxDog(dogC, dogD);In the code below, what are the dynamic types of
o,d,stuff[0], andstuff[1]?
Object o = new Dog("Hammy", "Beagle", 15);
Dog d = new ShowDog("Ammo", "Labrador", 54);
Object stuff[] = new Object[5];
stuff[0] = o;
stuff[1] = d;
studd[2] = null;Conceptual
Is it possible for an interface to extend a class? Provide an argument as to why or why not.
What are the differences between
extendsandimplementsinheritance? Is there a particular time when you would want to use one over the other?
Procedural
Say there is a class
Poodlethat inherits fromDog. The Dog class looks like this:
And the Poodle class looks like this:
Is this valid? If so, explain why. If it is not valid, then explain how we can make it valid.
The
Monkeyclass is a subclass of theAnimalclass and theDogclass is a subclass of theAnimalclass. However, a Dog is not a Monkey nor is a Monkey a Dog. What will happen for the following code? Assume that the constructors are all formatted properly.
How about for this code? Provide brief explanation as to why you believe your answers to be correct.
Metacognitive
Last updated