Introduction to Inheritance
- I saw one OO source that claimed this one of the foundations to OOP.
- This is chapter 10. Read it twice.
- Inheritance is the ability to extend one class to form a new class.
- He says "Without inheritance, classes would simply be data structures with associated behaviors."
- Some chapter 5 stuff
- Read starting on page 175.
- Composition or building one class with other classes as members can be expressed as a has a relationship.
- Or an is a part of
- There are several classes of this.
- A pond has a frog.
- Think vector of frogs as part of the pond class.
- There might be no frogs in the pond when it is first dug.
- The frog can exist outside of the pond.
- The pond can exist without a frog.
- This is aggregation
-
- A frog has a heart
- Think of a member component.
- When the frog is created, the heart member is created.
- When the frog is destroyed the heart member is also destroyed.
- This is Composition
-
- Of course, these do depend on the level of abstraction.
- In a frog-pond simulation, you might not allow a frog to exist outside of a pond.
- In a dissection simulation, you might allow a frog's heart to exist outside of a frog.
- In general you can discuss composition without either of these.
- But one will probably apply.
- You can specify a count on one or both sides.
-
- Here 0 to n frogs swim in 1 pond.
- It could include a * for unlimited.
- You will encounter diagrams like this in database as well.
- Inheritance
- This models an is-a
- In this case, we are not building something up, we are extending something.
- An Amphibian
- Has four legs.
- Have young through eggs
- Have lungs, however most can breathe through their skin.
- A frog is an amphibian
- Without a tail.
- Skin that secrete something (distasteful to toxic)
- A bullfrog is a frog
-
- As we move up this tree, we become more specialized.
- As we move down, we become more generalized.
- With inheritance we can
- Add functionality
- A frog adds the ability to ooze goo from its skin.
- A bullfrog adds an extra strong Biomechanical tongue.
- Replace functionality
- Adding Properties
- Replacing properties.
- We can also employ polymorphous when we have inheritance.
- You need to be careful not to define a hierarchy based on your perception
- He has a couple of examples on page 182.
- These are hierarchies in our world.
But probably not in the programming world.
- A CEO really is not a base class.
- He says to see if the different classes you design have different functionality.
- If not, they are not hierarchies, just organizational structures.
- This is where I have trouble making up examples, exercises, programs, ...
- Sometimes you might violate "real world" hierarchies
- You might organize things by how they move
- IE a dolphin and a penguin might be in a swimming class.
- They do share functionality.
- Multiple Inheritance allows mixing classes
- A penguin is derived from swimming animals ad from walking animals.
- But this leads to coding problems.
- We will discuss this later.