Monday, April 19, 2010

Object Oriented Programming: Inheritance


What is Inheritance?

In OOP, a parent or base class can inherit its behaviour and state to children or derived classes. Inheritance gives you the ability to build new classes based on an existing class. You can then extend a base class by enabling a new class to inherit its characteristics and behaviour.

Inheritance allows one class (the sub-class) to be based upon another (the super-class) and inherit all of its functionality automatically. Additional code may then be added to create a more specialised version of the class (Polymorphism).

For example a base class of vehicles can have sub-classes for cars or motorcycles. Each would still have all of the behaviour of a vehicle but can add specialised methods and properties. This mechanism of redefining the functionality of the base class in the derived class is called “overriding

Some programming languages allow for multiple inheritances where a sub-class is derived from two or more super-classes. C# does not permit this but does allow a class to implement multiple interfaces. An interface defines a contract for the methods and properties of classes that implement it. However, it does not include any actual functionality.

No comments: