Warning: is_readable(): open_basedir restriction in effect. File(C:\Inetpub\vhosts\techmonials.com\httpdocs/wp-content/plugins/C:/Inetpub/vhosts/techmonials.com/httpdocs/wp-content/themes/di-business/inc/kirki/kirki/kirki-en_US.mo) is not within the allowed path(s): (C:/Inetpub/vhosts//techmonials.com\;C:\Windows\Temp\) in C:\Inetpub\vhosts\techmonials.com\httpdocs\wp-includes\l10n.php on line 584
Object Oriented Programming (OOP) – Techmonials

Object Oriented Programming (OOP)

During the initial period of computer age, programming basically consists of logics, input and output of data. Logics were generally written in methods which gets some data as input, process the data based on some algorithms and return the processed data as output. In Non-OOP languages, there are mainly variables and methods present. Some Non-OOP languages are BASIC, FORTRAN, PASCAL, COBOL, C, etc.

Object Oriented Programming (OOP) is a concept which basically creates objects in programming which contains data and logic together. We can create classes, define variables and methods within classes, create instances of classes called objects. This way it makes programming easier and flexible.

What is a class?

A class is a prototype that can hold the attributes and functions of a certain type of objects. Suppose we want to define a class of car. The attributes (such as Company, Model, Year of manufacturing, colour, etc.) can be defined using variables, functions (such as Change gear, Turn left, Turn right, etc) can be defined using methods.

What is an object?

An object is an instance of a class which may initializes some properties with data. Basically when an object gets initialized, it allocates some memory in the Heap. We can set or get values to or from its properties available in the form of variable within the object. We can call its methods to perform some actions.

Benefits of OOP

  • Easy to design and develop
  • Easy to debug and maintenance
  • Reusability
  • Security

There are four basic principles of OOP. Those are encapsulation, abstraction, inheritance, polymorphism.

  1. Encapsulation

The whole idea of encapsulation is to hide the logic of algorithm implementation from user. For example, suppose an employee availed leave without pay in a month and a program need to be written to calculate his salary. So lets note down the points,

  • Employees monthly salary – data
  • Leave taken – data
  • Calculate payable salary – method

Here point to be noted is we don’t want to disclose the logic to calculate the payable salary to the user, hence we need to hide it. So the class should have the calculate method and it needs to be called from outside the class. So our class in C# should be like below.


2. Inheritance

This is a mechanism facilitates a programmer to define a base class that provides some data and behaviours and to define derived classes with same or overridden data and functionalities. Suppose there are some part time employees. They are hired from third party agencies and also eligible for over time payment. They also have the same properties of an Employee. So our derived class should look like


Here Part time employee class inherits Employee class, hence posses all the properties of Employee. This way, inheritance provides a mechanism for extensibility.3. Abstraction

This concept allows the programmer to define certain functionalities without defining in details. This allows efficient, extensible design and implementation of large and complex software system. In the previous class, the calculate salary method is common to all employees. Suppose there are some special employees whose salary calculation logic is different than others. To achieve this type of requirements, we need to modify our class as below


Here we have marked our Employee class as abstract. Abstract classes can’t be initialized directly. We need to create another class and inherit the Employee class. Then implement the abstract method. Our derived classes for special and general employees are as below


We can use these derived classes as below


Here essential information related to employee are monthly salary, leave taken and the calculated salary of the month. These are the fields defined in the Employee class but details of calculation of salary is missing. That is defined in the derived classes.

4. Polymorphism

This mechanism provides the ability to develop one object with different behaviours. There are basically two types of polymorphisms.

  • Static or compile time polymorphism
  • Dynamic or runtime polymorphism

Now lets discuss about these two types of polymorphisms briefly.

Static Polymorphism

This is also known as early binding. In method overloading, a method with the same name has defined multiple times but with different signatures. Operator overloading is also an example of static polymorphism.

This is an example of method overloading


Dynamic Polymorphism

This is also known as late binding. Method overriding is an example of Dynamic polymorphism. Here the method name and signature both are same but with different implementation. Below is an example




Here Area is a method defined in all classes. in base class it is defined as virtual and in derived classes it is overridden.

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Comments