BlackWasp
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.

Tutorial Prerequisites

The .NET Framework includes all of the software required to compile a program that can be developed using any standard text editor. However, Microsoft provides many development environments that are suitable and make programming and debugging much easier.

Microsoft Visual Studio is an ideal development environment for this tutorial if you have it available. If not, I strongly suggest downloading Microsoft's excellent, and free-of-charge, development environment, Microsoft C# Express Edition. However, if you have another preferred development environment for C# programming, you should easily be able to adapt the examples accordingly.

What is Object-Oriented Programming?

As computers increase in processing power, the software they execute becomes more complex. This increased complexity comes at a cost of large programs with huge codebases that can quickly become difficult to understand, maintain and keep bug-free.

Object-oriented programming (OOP) tries to alleviate this problem by creating networks of objects, each like a small software 'machine'. These objects are naturally smaller entities, simplifying the development task of each unit. However, when the objects co-operate in a system, they become the building blocks of much more complex solution.

Consider the motor car. If a car were designed as a single machine, it would be seen as hugely complex with lots of opportunities for failure. However, when broken down into its constituent parts, such as wheels, pedals, doors, etc. the individual design items become simpler. Each part (or object) is created independently, tested carefully and then assembled into the final product. The creation of the parts can be simplified further when they are broken down into even simpler items. For example, when each door is considered as being composed of an outer panel, handle, inner panel and window.

The car example is analogous to the object-oriented software. Rather than writing a huge program to create, for example, a project management system, the solution is broken into real-world parts such as project, task, estimate, actual, deliverable, etc. Each of these can then be developed and tested independently before being combined.

Key Concepts

This tutorial describes the use of object-oriented techniques with the C# programming language. Before we start to examine any sample code, it is important to review the ideas that will be discussed. Some of the key concepts are described in the following sections.

Classes and Objects

The basic building blocks of object-oriented programming are the class and the object. A class defines the available characteristics and behaviour of a set of similar objects and is defined by a programmer in code. A class is an abstract definition that is made concrete at run-time when objects based upon the class are instantiated and take on the class' behaviour.

As an analogy, let's consider the concept of a 'vehicle' class. The class developed by a programmer would include methods such as Steer(), Accelerate() and Brake(). The class would also include properties such as Colour, NumberOfDoors, TopSpeed and NumberOfWheels. The class is an abstract design that becomes real when objects such as Car, RacingCar, Tank and Tricycle are created, each with its own version of the class' methods and properties.

CarRacing CarTankTricycle
CarRacing CarTankTricycle
Colour: Red
NumberOfDoors: 2
NumberOfWheels: 4
TopSpeed: 60mph
Colour: Yellow
NumberOfDoors: 0
NumberOfWheels: 4
TopSpeed: 200mph
Colour: Green
NumberOfDoors: 1
NumberOfWheels: 12
TopSpeed: 40mph
Colour: Orange
NumberOfDoors: 0
NumberOfWheels: 3
TopSpeed: 50mph

Encapsulation

Encapsulation,also known as data hiding, is an important object-oriented programming concept. It is the act of concealing the functionality of a class so that the internal operations are hidden, and irrelevant, to the programmer. With correct encapsulation, the developer does not need to understand how the class actually operates in order to communicate with it via its publicly available methods and properties; known as its public interface.

Encapsulation is essential to creating maintainable object-oriented programs. When the interaction with an object uses only the publicly available interface of methods and properties, the class of the object becomes a correctly isolated unit. This unit can then be replaced independently to fix bugs, to change internal behaviour or to improve functionality or performance.

In the car analogy this is similar to replacing a headlight bulb. As long as we choose the correct bulb size and connection (the public interface), it will work in the car. It does not matter if the manufacturer has changed or the internal workings of the bulb differ from the original. It may even offer an improvement in brightness!

Passing Messages

Message passing, also known as interfacing, describes the communication between objects using their public interfaces. There are three main ways to pass messages. These are using methods, properties and events. A property can be defined in a class to allow objects of that type to advertise and allow changing of state information, such as the 'TopSpeed' property. Methods can be provided so that other objects can request a process to be undertaken by an object, such as the Steer() method. Events can be defined that an object can raise in response to an internal action. Other objects can subscribe to these so that they can react to an event occurring. An example for vehicles could be an 'ImpactDetected' event subscribed to by one or more 'AirBag' objects.

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.

Link to this Page15 September 2007
RSS RSS Feed