Introduction to object oriented programming


Object oriented programming is a style of programming where the code and data are bound together in objects, whose structures are base on class hierarchies.

You can also define object oriented programming as a collection of cooperating objects, each object is capable of receiving messages, processing data, and sending messages to other objects.

Each object can be viewed as an independent little machine with a distinct role.

Let's start the story from the beginning:

In any program you work with data, the program receive the data as input from the user, processing the data, and return the data as output to the user.

This data can be of any type like (numbers, text, boolean expression, etc) so when we dealing with data we store it in variables, any variable Should be of a certain data type that represents the type of data it can store.

The data type also defines the characteristics and behavior of the variable.

The .NET Framework has many data types you can use like:

int: for integer numbers
string: for words and text
bool: true or false
char: for characters
and so on

So if you want to store any data in a variable the variable must be of data type that suitable for this data. For example if you want to store an integer number in a variable we first create a variable of int.

int myNumber;

As you saw, to create our variable we start with the keyword that represents the data type (int) and then the name we gave to our variable. Then we can store our number in the variable.

myNumber = 55;

and the same thing with other data types.

myAge = 23;
string myName = "Amr Ashush";
char initials = 'M';
bool isAuthor = true;

User Defined Data types and Objects:

What if you want a data type that represents something in real world like (Car, Person, Employee, or any thing). As you may know these types doesn't exist in .NET.

If you want to deal with a thing (object) like a car in your program you need to define its data type (class) that defines its characteristics and behavior.

The Class Type:

A class is a custom user defined data type that defines the characteristics of a thing (object's fields or properties) and the behavior of the thing (methods).

Object-oriented languages allow the programmers to define data types that represent real world entities by grouping data and functionality in a single User Defined Type (UDT).

Let's see an example of creating Employee class (UDT):

//Our UDT Employee

public class Employee


    //the class characteristics

    public string firstName;

    public string lastName;

    public int Age;

    public int ID;


    //the class behavior

    public void DoWork()


        Consol.WriteLine("Employee {0} {1} doing his work", firstName, lastName);



    //the default constructor

    public Employee() { }

As you saw we created a Employee class which define the Employee characteristics (an employee has first name, last name, age, and ID) and define also Employee behavior or what Employee can do (DoWork() method)

We now defined our data type let's see how we can use it to create Objects.


//creating the first object

Employee firstEmployee = new Employee();


//adding data to the first object

firstEmployee.firstName = "Amr";

firstEmployee.lastName = "Ashush";

firstEmployee.Age = 23;

firstEmployee.ID = 12345;


//creating the second object

Employee secondEmployee = new Employee();


//adding data to the second object

secondEmployee.firstName = "Jack";

secondEmployee.lastName = "Bauer";

secondEmployee. Age = 40;

secondEmployee.ID = 125;

//Ask the first Employee to do work

The result:

Amr Ashush doing his work.

//Ask the second Employee to do work

The result:

Jack Bauer doing his work.

As you can see we created tow objects (Employees) each one has its own data and behavior. So as I said previously each object can viewed as independent little machine with a distinct role and responsibility. So when we assign any data to an object it assigned only to this object unless there is some objects points to the same value in the heap(will talk about this later).

The main pillars of Object-Oriented Programming:

Any object-oriented language has three main pillars:

  1. Encapsulation: The ability of hiding object's internal implementation.
  2. Inheritance: The ability of building a new class definition based on existing class definition.
  3. Polymorphism: The ability of treating related objects the same way.

First pillar of OOP -Encapsulation:

Encapsulation gives you the ability to hide unnecessary implementation details from the object user. It simplifies the programming tasks instead of writing many lines of code.

As you saw in the last example all we need to is create an object and call the do work method instead of writing all lines of code in the DoWork() method each time we want the object do the same functionality.

Another benefit of encapsulation is data protection so we must define object's data as private so the object user must ask in order to change or obtain the value so we can make all validation processes we need.

For more information about Encapsulation see my article:

The first pillar of object-oriented programming -Encapsulation

Second pillar of OOP -Inheritance:

Inheritance gives you the ability of building a new class definition based on existing class definition. Inheritance allows you to extend the behavior of a base class by enabling a sub class to inherit its core functionality.

For example our we can extend our class Employee by allow a sub class named SalesEmployee to inherit it

//This class has the same functionality of the Employee class
public class SalesEmployee : Employee

Third pillar of OOP -Polymorphism:

Polymorphism gives you the ability of treating related objects the same way. Polymorphism allow the base class to define a set of members to all descendents, when derived types override the members defined by the base class, they are redefining how they respond to the same request.