Checkpoint: The Zoo Census
beginner45 min readLesson 39 of 180
An abstract base, an extra interface capability, and an instanceof census — identity and capability side by side.
The Zoo: one abstract base, three species, and a census.
Inside Solution:
static abstract class Animal—abstract String sound();and apublic String toString()override returningsound()(so printing shows the sound).static class Dog extends Animal— sound"woof".static class Cat extends Animal— sound"meow".static class Robot extends Animal— sound"beep", but Robot is NOT really an animal, so it must alsoimplements Describablewhereinterface Describable { String kind(); }returns"machine".static int countRealAnimals(Animal[] zoo)— the number of animals in the array that implementDescribable... is zero logic: instead count the ones whosesound()is a real animal sound. Simpler and honest: count entries that are NOT instanceofDescribable(that is exactly the interface check the lesson allows here).
So for { new Dog(), new Cat(), new Robot() }, countRealAnimals returns 2.
The grader also checks polymorphism: every sound() must dispatch to the
object's own class.