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], and stuff[1]?
Object o =newDog("Hammy","Beagle",15);Dog d =newShowDog("Ammo","Labrador",54);Object stuff[] =newObject[5];stuff[0] = o;stuff[1] = d;studd[2] =null;
Problem 1
The static type is Dog, the declared return type of the maxDog method.
Problem 2
Only the first line compiles. The static return type of maxDog is Dog, a superclass of ShowDog, so the compiler assumes that this method call return value is too broad for our declared variable msd.
Dog md =Dog.maxDog(dogC, dogD);// ShowDog msd = Dog.maxDog(dogC, dogD);
Problem 3
o: Dog, since it is instantiated as a Dog.
d: ShowDog, since it is instantiated as a ShowDog.
stuff[0]: Dog, since it points to the same object as o, which has dynamic type Dog.
stuff[1]: ShowDog, since it points to the same object as d, which has dynamic type ShowDog.
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 extends and implements inheritance? Is there a particular time when you would want to use one over the other?
Problem 1
This is not possible in Java. Conceptually, the reasoning is that classes have implementation details for each method, whereas interfaces do not implement methods. Thus, there is no way for an interface to "inherit" implementations from a superclass.
Problem 2
The primary difference is that extends inherits implementations, allowing a subclass to reuse code from the superclass, while implements only serves as a "contract" that a class will implement certain methods.
Use extends when you want a subclass to have the same behavior as a superclass in certain methods. Use implements when subclasses have different implementations of the same conceptual methods, or when you need to inherit from multiple supertypes.
Procedural
Say there is a class Poodle that inherits from Dog. The Dog 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 Monkey class is a subclass of the Animal class and the Dog class is a subclass of the Animal class. 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.
Monkey jimmy =newMonkey("Jimmy");Dog limmy = (Dog) jimmy;
How about for this code? Provide brief explanation as to why you believe your answers to be correct.
This will not compile because the Poodle constructor implicitly calls the Dog constructor (through super), but the Dog class has no zero-argument constructor.
To make this valid, we could add a zero-argument Dog constructor, or make the Poodle constructor take in an int argument and call super(weight_in_pounds).
Problem 2
This will not compile, since casting to a "sibling" class (same parent class, but unrelated), will error during compile.
Problem 3
This compiles, since the static types match up: mangotan is declared as a Dog and cast to a Dog on the right-hand side. However, the dynamic type of orangutan is a monkey, and we will run into a casting error since the Monkey class is unrelated to the Dog class.