๐ WAYPOINT LESSON
Checkpoint: The Zoo Census
โญ beginnerโณ 45 min read๐ Lesson 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.