James Ray Anderson

James Ray Anderson
James Ray Anderson
0 comments

Quick and Easy OOP - Part IV - Inheritance

8:00 PM
Overview
In programming the Inheritance is a way to re-use the code of an existing object, or to create a subtype of that object.  It also expresses a relationship between classes. 

Think of it as a trait hierarchy.  The child inherits traits from the parent.  These traits are "properties" and "methods".

Classes as Templates
You can use one class as a template for creating another class, from which you can then add your own items too.  This allows you to re-use code and simplify development while maintaining consistency.

Types of Classes - Parent
There are some definitions that specify the class in the hierarchy.  The "parent level" is often referred to as the Base Class or Superclass.  The "base class" and "superclass" mean the same thing. 

Types of Classes - Child
The "child level" inherits the properties and methods of the "parent level".  The "child level" is often referred to as the child, derived, or subclass.  The parent is the template by which the child will build off of.

Does Inheritance make sense?
That depends on the relationship.  Building a relationship between a stapler and a cat don't make sense.  The common phrase you can use to determine viability of a relationship is the "is a".

For example:
  • apple is a fruit
  • fruit is a food
  • chocolate cake is a cake

In the case of a stapler and a cat, a stapler is not a cat, so using Inheritance wouldn't apply.

0 comments:

 
Toggle Footer
Top