Skip to content

Commit cac432c

Browse files
committed
Initial Commit
1 parent 5ee7929 commit cac432c

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

_posts/2015-10-14-pillars-of-oop-inheritance.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ Back for some more PIE? Good. This time, let's talk about...
2424

2525
So we mentioned inheritance in [part one]({% post_url 2015-10-07-pillars-of-oop-polymorphism %}) of this series, and for a good reason. If `Dog` couldn’t *inherit* from `Animal`, we would’t have been able to accomplish what we just did - which was storing `Dog` in an array of `Animal`s.
2626

27-
So what does inheritance do for us? Well, it allows us to create classes (called a derived, child, or subclass) using other classes (called a base, parent, or super class). This creates an **is-a** relationship - a derived class (`oHuman`, `oPet`, `oDog`, `oCat`) **is-a** base class (`oAnimal`). On the flip side, a `oHuman` **is not a** `oPet`.
28-
![Image of inheritance](http://www.derekyu.com/tigs/forums/tutorials/gmtut/gmtut-008.png)
29-
{: .image-pull-right}
30-
This makes it easier for us to reuse code (because good programmers are lazy) and create software that is flexible and easily maintainable. For example, take a gander to the image on the right. Imagine that we’re creating a game involving walking humans, dogs, and cats. We did our market research and found that walking animals aren’t what the hip, cool kids are looking for nowadays - they want [flying animals](http://i.ytimg.com/vi/QH2-TGUlwu4/hqdefault.jpg). Well, there’s two ways of doing this: 1) adding flight capability to each class (`oHuman`, `oDog`, and `oCat`), or 2) adding `flight = true` to our parent class (`oAnimal`), have the change trickle down to it’s child (and grandchildren) classes, and be on our merry way. What would you chose? 1 or 2?
27+
<figure>
28+
<a href="http://www.derekyu.com/tigs/forums/tutorials/gmtut/gmtut-008.png"><img src="http://www.derekyu.com/tigs/forums/tutorials/gmtut/gmtut-008.png"></a>
29+
<figcaption>”The tree of life.”</figcaption>
30+
</figure>
31+
32+
So what does inheritance do for us? Well, it allows us to create classes (called a derived, child, or subclass) using other classes (called a base, parent, or super class). This creates an **is-a** relationship - a derived class (`oHuman`, `oPet`, `oDog`, `oCat`) **is-a** base class (`oAnimal`). On the flip side, a `oHuman` **is not a** `oPet`. This makes it easier for us to reuse code (because good programmers are lazy) and create software that is flexible and easily maintainable. For example, take a gander to the image on the right. Imagine that we’re creating a game involving walking humans, dogs, and cats. We did our market research and found that walking animals aren’t what the hip, cool kids are looking for nowadays - they want [flying animals](http://i.ytimg.com/vi/QH2-TGUlwu4/hqdefault.jpg). Well, there’s two ways of doing this: 1) adding flight capability to each class (`oHuman`, `oDog`, and `oCat`), or 2) adding `flight = true` to our parent class (`oAnimal`), have the change trickle down to it’s child (and grandchildren) classes, and be on our merry way. What would you chose? 1 or 2?
3133

32-
So you chose #2, [you must be a software developer](https://bintrayblog.files.wordpress.com/2013/10/lazyness.jpg?w=630). Inheritance comes in two flavors: single and multiple. An example of *single inheritance* is a class that inherits from a single base class - above, `oDog` only inherits from `oPet`. You can probably guess what *multiple inheritance* means - a class that inherits from 2 or more base classes (e.g. `Child` inherits from `Father` and `Mother`).
34+
So you chose #2, [you must be a software developer.](https://bintrayblog.files.wordpress.com/2013/10/lazyness.jpg) Inheritance comes in two flavors: single and multiple. An example of *single inheritance* is a class that inherits from a single base class - above, `oDog` only inherits from `oPet`. You can probably guess what *multiple inheritance* means - a class that inherits from 2 or more base classes (e.g. `Child` inherits from `Father` and `Mother`).
3335

3436
Here’s a code example of multiple inheritance:
3537

0 commit comments

Comments
 (0)