Showing posts with label Sub-class. Show all posts
Showing posts with label Sub-class. Show all posts

Wednesday, October 27, 2010

Inheritance

Classes contain state & behaviour - things they have & things they do. Just as humans inherit certain states & behaviours from their parents, so too can classes.


Often referred to as a Parent-Child relationship. Other names include:

Parent | Superclass | Base class | Generalisation

Child | Subclass | Derived class | Specialisation

Inheritance

Inheritance is said to be an is-a relationship. In the example below we can see 4 classes - but actually, the diagram illustrates that there is one superclass and three sub-classes.

Level is the superclass. WaterWorld, RainbowWorld and DragonWorld are sub-classes which have inherited state and behavior from the super class, as well as having their own unique state and behavior.


In plain English we can say that a WaterWorld object is-a Level object, a RainbowWorld object is-a Level object and a DragonWorld object is-a Level object

Inheritance is a feature supported by many programming languages and lies at the heart of Object Oriented Design.

The example above shows the correct notation to illustrate inheritance in a class diagram.