From the October 2015 Issue of Prestwood eMag
 
Coder Object Orientation (OO):
Inheritance
 
Posted 1/23/2008 on 1/23/2008 and updated 1/5/2009
Take Away:

The concept of a class makes it possible to define subclasses that share some or all of the main class characteristics. This is called inheritance. Inheritance also allows you to reuse code more efficiently. In a class tree, inheritance is used to design classes vertically. (You can use Interfaces to design classes horizontally within a class tree.) With inheritance, you are defining an "is-a" relationship (i.e. a chow is-a dog). Analysts using UML call this generalization where you generalize specific classes into general parent classes.

Coder Definition Flashcard:

KB100813

Inheritance -

The concept of a class makes it possible to define subclasses that share some or all of the main class characteristics. This is called inheritance. Inheritance also allows you to reuse code more efficiently. In a class tree, inheritance is used to design classes vertically. (You can use Interfaces to design classes horizontally within a class tree.) With inheritance, you are defining an "is-a" relationship (i.e. a chow is-a dog). Analysts using UML call this generalization where you generalize specific classes into general parent classes.


Inheritance is a type of class or object association. The concept of a class makes it possible to define subclasses that share some or all of the main class characteristics. This is called inheritance. Inheritance also allows you to reuse code more efficiently.

Designing with Inheritance

In a class tree, inheritance is used to design classes vertically. (You can use Interfaces to design classes horizontally within a class tree.) With inheritance, you are defining an "is-a" relationship (i.e. a chow is-a dog). Analysts using UML call this generalization where you generalize specific classes into general parent classes or take general parent classes and specialize them as needed in child classes.

Inheritance is Generalization

Inheritance is Generalization and the terms can almost be used synonymously. In general, I prefer to use the term generalization when talking to analysts trying to understand a problem domain and inheritance when talking to programmers who need to implement an OOP solution.

UML Symbol

UML Generalization Arrow

 

Inheritance-Based Polymorphism

A.k.a. Overriding, Inclusion Polymorphism, Runtime Polymorphism, Dynamic Binding, or Late Binding

A coding element which involves defining methods in a parent class and overriding them with new implementations in child classes. For example, you could define a method in a parent class and override it in descendant classes as needed. When using an object instantiated from one of these classes, you don't actually need to know which method implimentation is used, you just need to know the name of the method or property to use.

Polymorphism allows the software architect to design a dog.run and a cat.run method and then the programmer can decide at development time whether to instantiate a dog or cat object and either way he knows to call the run method to make the object run. If the designer created a MakeDogRun and a MakeCatRun methods, then the programmer would have a hard time remembering different commands and could not easily substitute one for the other.

Code Examples

Refer to the links below for code examples in various languages.

Definition Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile
www.prestwood.com For service: 916-726-5675
or support@prestwood.com
Copyright (C) Prestwood IT Solutions.
All Rights Reserved.
Printed 1/27/2022