Chapter 7. Interfaces and Abstract Classes: Making classes keep their promises

image with no caption

Actions speak louder than words.

Sometimes you need to group your objects together based on the things they can do rather than the classes they inherit from. That’s where interfaces come in—they let you work with any class that can do the job. But with great power comes great responsibility, and any class that implements an interface must promise to fulfill all of its obligations...or the compiler will break their kneecaps, see?

Let’s get back to bee-sics

The General Bee-namics corporation wants to make the Beehive Management System you created in the last chapter into a full-blown Hive Simulator. Here’s an overview of the specification for the new version of the program:

image with no caption
image with no caption

Lots of things are still the same

The bees in the new Hive Simulator will still consume honey in the same way they did before. The queen still needs to be able to assign work to the workers and see the shift reports that tell who’s doing what. The workers work shifts just like they did before, too; it’s just that the jobs they are doing have been elaborated a little bit.

We can use inheritance to create classes for different types of bees

Here’s ...

Get Head First C#, 3rd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.