BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Programming Concepts

Object-Oriented Programming Concepts

This is the first in a series of articles describing the use of object-oriented programming techniques as implemented by the C# programming language. This series follows the C# Fundamentals tutorial that beginners are advised to read first.

Abstraction

The vehicle class described above is an example of abstraction. Abstraction is the process of representing simplified versions of real-world objects in your classes and objects. The car class does not describe every possible detail of a car, only the relevant parts for the system being developed. Modelling software around real-world objects can vastly reduce the time required to understand a solution and be able to develop and maintain it.

Composition

Objects can work together in many ways within a system. In some situations, classes and objects can be tightly coupled together to provide more complex functionality. This is known as composition. In the car example, the wheels, panels, engine, gearbox, etc. can be thought of as individual classes. To create the car class, you link all of these objects together, possibly adding further functionality. The internal workings of each class are not important due to encapsulation as the communication between the objects is still via passing messages to their public interfaces.

Inheritance

Inheritance is an interesting object-oriented programming concept. It 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. In the example of vehicles, sub-classes for cars or motorcycles could be created. Each would still have all of the behaviour of a vehicle but can add specialised methods and properties, such as 'Lean()' and 'LeanAngle' for motorcycles.

Some programming languages allow for multiple inheritance 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.

Polymorphism

Polymorphism is the ability for an object to change its behaviour according to how it is being used. Where an object's class inherits from a super-class or implements one or more interfaces, it can be referred to by those class or interface names. So if we have a method that expects an object of type 'vehicle' to be passed as a parameter, we can pass any vehicle, car or motorcycle object to that method even though the data type may be technically different.

Modularity

In addition to the concepts described above, object-oriented programming also permits increased modularity. Individual classes or groups of linked classes can be thought of as a module of code that can be re-used in many software projects. This reduces the need to redevelop similar functionality and therefore can lower development time and costs.

15 September 2007