# 10.5 Exercises

## Factual

Recall that the `maxDog` method has the following signature:&#x20;

```java
public static Dog maxDog(Dog d1, Dog d2) { … }
```

1. What is the static type of `Dog.maxDog(dogC, dogD)`?

```java
ShowDog dogC = new ShowDog("Franklin", "Malamute", 180, 6);
ShowDog dogD = new ShowDog("Gargamel", "Corgi", 44, 12);

Dog.maxDog(dogC, dogD);
```

2. Which (if any), will compile:&#x20;

```java
Dog md = Dog.maxDog(dogC, dogD);
ShowDog msd = Dog.maxDog(dogC, dogD);
```

3. In the code below, what are the dynamic types of `o`, `d`, `stuff[0]`, and `stuff[1]`?

```java
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;
```

<details>

<summary>Problem 1</summary>

The static type is `Dog`, the declared return type of the `maxDog` method.

</details>

<details>

<summary>Problem 2</summary>

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`.&#x20;

```java
Dog md = Dog.maxDog(dogC, dogD);
// ShowDog msd = Dog.maxDog(dogC, dogD);
```

</details>

<details>

<summary>Problem 3</summary>

* `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`.&#x20;
* `stuff[1]`: `ShowDog`, since it points to the same object as `d`, which has dynamic type `ShowDog`.

</details>

## Conceptual

1. Is it possible for an interface to extend a class? Provide an argument as to why or why not.
2. What are the differences between `extends` and `implements` inheritance? Is there a particular time when you would want to use one over the other?

<details>

<summary>Problem 1</summary>

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.

</details>

<details>

<summary>Problem 2</summary>

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.

</details>

## Procedural

1. Say there is a class `Poodle` that inherits from `Dog`. The Dog class looks like this:

```java
public class Dog {
  int weight;
  public Dog(int weight_in_pounds) {
    weight = weight_in_pounds;
  }
}
```

And the Poodle class looks like this:

```java
public class Poodle extends Dog {
 public Poodle() {}
}
```

Is this valid? If so, explain why. If it is not valid, then explain how we can make it valid.

2. 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.

```java
Monkey jimmy = new Monkey("Jimmy");
Dog limmy = (Dog) jimmy;
```

3. How about for this code? Provide brief explanation as to why you believe your answers to be correct.

```java
Monkey orangutan = new Monkey("fruitful");
Dog mangotan = ((Dog) ((Animal) orangutan));
```

<details>

<summary>Problem 1</summary>

This will not compile because the `Poodle` constructor implicitly calls the `Dog` constructor (through `super`), but the `Dog` class has no zero-argument constructor.&#x20;

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)`.

</details>

<details>

<summary>Problem 2</summary>

This will not compile, since casting to a "sibling" class (same parent class, but unrelated), will error during compile.

</details>

<details>

<summary>Problem 3</summary>

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.

</details>

## Metacognitive

1. [Problem 1](https://drive.google.com/file/d/14yQDPQVcsWAG-QBPxlrX3N-YDRyR2c_C/view) from the Spring 2018 Midterm 1
2. [Problem 1](https://drive.google.com/file/d/12KqULT9XIb3KHQqOdGQprsKELYTrAXN5/view?usp=sharing) from the Spring 2017 Midterm 1

<details>

<summary>Problem 1</summary>

[Solutions](https://drive.google.com/file/d/1hnRfUh2ImLGqwOSS28hFUxnpZ_cjMaQT/view) and [walkthrough](https://www.youtube.com/playlist?list=PLnp31xXvnfRqAfvA4R9Oh09PstFymwCif) are linked here and on the course website.

</details>

<details>

<summary>Problem 2</summary>

[Solutions](https://drive.google.com/file/d/15Vnd9z34Wlp-D9boc4ZezgpyH_ul65qZ/view?usp=sharing) and [walkthrough](https://www.youtube.com/playlist?list=PLnp31xXvnfRr58vXF7T9I2HZ1LrVlxCtG) are linked here and on the course website.

</details>

