Object Oriented Programming and C# .NET

Object-Oriented Programming (OOP) is a philosophy for designing programming languages that has produced a very different set of languages from older procedural programming languages such as C and Pascal.

Prove you're a Dev Guru! Take the test now!

The framework of OOP is built up from different classes of 'object'. These classes and objects can easily correlate to real-life scenarios.

As an analogy, consider the concept of a class called 'Vehicle'. This class would have properties such as Colour, NumberofDoors, NumberofWheels, MaximumSpeed, MaximumAcceleration. This class would also include methods such as Start(), Stop(), Accelerate().

Class Vehicle

{

Color VehicleColour;

int NumberofDoors;

int NumberofWheels;

int MaximumSpeed;

int MaximumAcceleration;

public void Start()

{

//code to start the vehicle

}

public void Stop()

{

//code to stop the vehicle

}

public void Accelerate()

{

//code to accelerate the car

}

}

This is a blueprint that only becomes real when objects such as Car, Bus, Bike and Scooter are initiated, each with its different version of the methods and properties that are associated with the class.

Any programming language that has data and methods encapsulated as objects is referred to as 'object-oriented'. Object-oriented programming methodology has many benefits such as the re-usability of code, high efficiency, flexibility and ease of maintenance.

Any object-oriented language supports the following four paradigms:

1. Abstraction

2. Encapsulation

3. Inheritance

4. Polymorphism

Abstraction in C#

Abstraction refers to the act of showing only essential features, without including the background details. In C# we have classes that use the concept of abstraction. Classes store data and methods and only the essential methods and properties are exposed to the outside world. The data stored in a class can only be accessed by these properties and methods. For e.g.:

Class Car

{

private string registrationNumber;

private int cost;

public int GetCost()

{

return cost;

}

public string GetRegistrationNumber()

{

return registrationNumber;

}

}

In the example above, the data (cost and registrationNumber) is not accessible from outside class; it can only be used by the methods (GetCost, GetRegistrationNumber) that are exposed.

Encapsulation in C#

Storing data and methods in a single unit is called encapsulation. In C#, encapsulation is implemented using classes. A class can contain data and methods. In the above example, the encapsulation is realised by the class Car, which encapsulates the data members RegistrationNumber and cost, and the methods GetCost and GetRegistrationNumber.

Inheritance in C#

Inheritance refers to the way in which objects can acquire the properties of other, 'parent' classes. This feature enables code to be re-used, so that further methods and data can be added to an existing class without modifying it. For example:

Class Mercedes: Car

{

private int colourCode;

public int GetColourCode()

{

return colourCode;

}

}

Now the class Mercedes possesses the methods of base class Car, as well as a new method GetColourCode.