This is part 4 of a series. To get the most out of these posts, read them in order:
Part 4: Review of Objects, Classes and Inheritance
We’ve been over several Object Oriented Programming (OOP) concepts in the last 3 posts, so it’s time to recap and take a quiz!
Let’s combine all of those concepts by imagining a program that would help Santa deliver presents.
A Function (or Method) might look something like this:
Deliver Present From Deliverer To Recipient
Procedural Programming is just a list of step-by-step instructions. If you say, “Deliver Elf From Present to Child”, the program says, “Yes, ma’am!” and you have a child wondering why a present just gave him an elf.
That would be awkward, so we’re going to use OOP instead. That will allow us to treat each noun as an object, and restrict what those objects can and can’t do.
First, let’s make our list of Objects:
- Elfie the Elf
- Buddy the Elf
- Toy Car
- Rag Doll
- Little Jimmy
- Little Janey
- Jolly the Elf-Child
Now, let’s break those Objects down into Classes:
- Deliverers: Santa, Elfie, Buddy
- Presents: Car, Doll
- Recipients: Jimmy, Janey, Jolly the Elf-Child
These classes work fine for our current method, but what if we add more methods like “Choose Recipient for Present” or “Make Present”? We don’t want Santa making presents (never give him a hammer), and we don’t want elves making decisions about presents (Wooden shoes for everyone!).
We also don’t want to have to write all our delivery-related methods twice, and imagine if we get desperate and have to start using reindeers and snowmen to make deliveries as well? We’re going to be re-writing the same methods over and over!
It’s time to make some sub-classes using Inheritance.
Parent Class: Deliverers
Methods: “pick up present”, “go down chimney”, “place present under tree”
Elf Class (Inherits from Deliverer Class)
Methods: Deliverer methods + “Make presents”
Santa Class (Inherits from Deliverer Class)
Methods: Deliverer methods + “Choose Recipient for Present” + “Say ‘Ho ho ho!'”
Now let’s look at Elfie. Elfie is an Elf, and Elves are Deliverers. That means Elfie is an Elf AND a Deliverer.
But what about Jolly the Elf-Child?
Jolly can receive presents, because he is a child, but when the workshop is behind schedule, Santa might need him as a deliverer or toy-builder too. How do we handle that?
One way is through Multiple Inheritance. Jolly can Inherit from both the Elf Class and the Child Class. But…remember how that can get messy if they have different methods with the same name?
Let’s use Single Inheritance instead.
We’ll create an Elf-Child class that inherits from Elf, and make a Module with the important Child methods.
Module methods: “receive presents”, “flip out over presents”, “believe in Santa”
That module is a “Mixin”, because we are “mixing it into” a class it would not normally be part of.
Now You Try:
(No wrong answers, just exercises in thought):
Mrs. Claus can deliver presents, make decisions about who receives presents, and also bake cookies.
- In the above situation, what are the Objects?
- What Methods Does Mrs. Claus have?
- Would she belong to an existing class, or would you create a new class?
- With Multiple Inheritance, what classes could a Mrs. Claus Class inherit from?
- If you used Multiple Inheritance, what methods could overlap between those Parent Classes (for example, could a child and an elf sleep differently?)
- With Single Inheritance, what one class would you choose for Mrs. Claus to inherit from?
- What other methods would you “Mix in”?
Feel free to leave a comment with your take on the answers to these questions. I’ll be happy to give you feedback.
However, to be fair to yourself and others, please:
- Answer the questions for yourself before reading others’ answers
- If you comment on someone else’s answer, please make sure it is “constructive criticism” (Instead of saying “You’re wrong!”, nicely explain how the answer could have been improved in your humble opinion.)
We are all Big Kids here, and I do not want to have to put any commenters in Time Out.
Want to Learn More? See Part 5: Encapsulation