Hello friends in this tutorial we are going to learn what is Object Oriented Programming in C#.

Introduction:

As we know there are three commonly used programming paradigms:

  • Procedural programming.
  • Functional programming
  • Object-oriented programming.

C# has the support for both procedural and object-oriented programming. Object Oriented Programming uses objects and their interaction to design Program application.

In Object Oriented Programming there are four main pillars:

  • Abstraction
  • Polymorphism
  • Encapsulation
  • Inheritance

Object Oriented Programming

Why we need Object Oriented Programming?

Before OOPs concepts were introduced, there was “Procedural” programming, that uses a bunch of functions and procedures while designing a software system. This makes the Software System more complex and hard to understand to maintain the software system.

What is Object Oriented Programming?

The OOP came with the new concepts of designing Software System. In Object Oriented Programming the software system is divided into small units called object and then builds data and function around these objects, which rise up decoupling and increases code re-usability. A most common use of decoupling in OOP is to polymorphically decouple the encapsulation. For code reusability, it is introduced by Inheritance and Generics concepts.

Object-oriented programming (OOP) tries to lighten the 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 the much more complex solution.

Object Oriented Programming

Features of Object Oriented Programming:

Class:

A class is the blueprint of the object that uses variable for storing data and functions to perform operations on that data. It acts as a template for other classes. Since a Class is a logical representation of data so it does not occupy any space in the memory. We can simply create a class by using the Class keyword. Here is the example for creating a Class in C#.

Class MyClass

{

//Functionality goes here

}

Object:

Object Oriented Programming is all around Object, which means that it is the basic building block of C# Program. An object is anything in that exists in real world and can perform set of related operations that describe a behaviour of the object. For example, an Employee can have Employee ID and Name. An Object contains the data and methods called the member of the object.

Memory is not allocated for the class until we do not create an object of the class using the new operator. When we create an object of the class without using new operator the memory will not be allocated in the heap.

Suppose there is a class called Employee.

class Employee

{

//Functionality Goes here

}

Here is the syntax for creating an object of the class.

Employee objEmployee=new Employee();

You can see we have used “new” keyword to create an object of Employee class.

Object Oriented Programming

Abstraction:

Abstraction is the process of showing essential data and without showing the implementation. This means that Abstraction describes what an object does not how it does. We can achieve Abstraction using one of the features provided by C# like Interfaces, Abstract Classes, Inheritance and Encapsulation.

Example:

No need to go far way from programming. You can find a good example of abstraction in .Net framework, for example, List and collection. You use List and collection in most of the scenario in C#. These are the most abstract classes provided by .Net framework.

Object Oriented Programming

Let’s make it more clearly by a real world example:

In today’s life, all of us have a cell phone. Some people have a cell phone with facilities like calling and sending messages and some people have a cell phone with facilities like calling, sending messages, FM radio, Camera and some have a cell phone with calling, sending messages, FM radio, camera, Video Recording and Sending and Receiving Emails.

Let’s understand above example into Programming Language.

class ModelA01

{

Calling();

SendingAndReceivingMessages();

}

class ModelA02: ModelA01

{

Calling();

SendingAndReceivingMessages();

FMRadio();

Camera();

}

class ModelA03: ModelA01

{

Calling();

SendingAndReceivingMessages();

FMRadio();

Camera();

VideoRecording();

SendingAndReceivingEmails

}

You can see ModelA01 shares common functionalities with other two model ModelA02 and ModelA03. So Abstraction is the common thing.

Encapsulation:

As now you can understand Abstraction is the process of showing the essential details. On the other hand, Encapsulation is the process of hiding essential detail outside the class and interface. Encapsulation is used to wrapping up data member and member function into a single unit such as class. Encapsulation is like your wallet in which you carry your money, Debit card, Credit card and Visiting card.

class wallet

{

Money;

DebitCard;

CreditCard;

VistingCard;

WithdrawlMoneyUsingDebitCard();

}

This means that encapsulation is the processing hiding confidential details from outside the world.

Real World Example:

Suppose when you will go to purchase a refrigerator from a shop, the sales person explains you about the capacity of the refrigerator, colour and how to use refrigerator now how it works. This process of hiding internal details is called Encapsulation.

Object Oriented Programming

Let’s take a look at another example:

Suppose a company don’t want to expose contact details of Employee by exposing only the necessary information. We can achieve this scenario using Encapsulation.

class Employee

{

public string FirstName;

public string LastName;

protected string MobileNo;

protected string Address;

public string GetEmployeeName()

{

Return FirstName+” “+LastName;

}

}

In the above example, we only expose Name of the Employee. We have declared MobileNo and Address as protected because we do not want to expose these details.

Inheritance:

Inheritance is the property through which a child class can access members of the parent class. Using inheritance we can extend the functionality of one class to other class. In this way, a class can act as a template for other classes. Inheritance provides us feature such as code reusability.

Object Oriented Programming

Let’s take a look at below code:

  public class Shape

    {

        public Shape()

        {

            Console.WriteLine("Parent Class Constructor.");

        }

        public void printMessage()

        {

            Console.WriteLine("I'm a method of Shape Class.");

        }

    }

    public class Triangle: Shape

    {

        public Triangle()

        {

            Console.WriteLine("Triangle Class Constructor.");

        }

        public static void Main()

        {

            Triangle objTriangle= new Triangle();

            objTriangle.printMessage ();

        }

    }

Note: C# does not support multiple inheritances but it does support multi-level inheritance.

Polymorphism:

Polymorphism means many forms of one function. In polymorphism, the same function can behave in different forms.

A good example of polymorphism is your mobile phone one name many forms. It can behave:

  • As a Phone.
  • As a Camera.
  • As an MP3 Player.
  • As an FM radio.

Another example of polymorphism is a CAB which can be A.C. or Non A.C.

Object Oriented Programming

Let’s take a look at a C# Program.

class PolymorphismExample

{

Public int Sum(int Number1,int Number2)

{

Return Number1+Number2;

}

Public int Sum(int Number1,int Number2,int Number2)

{

Return Number1+Number2+Number3;

}

}

You can see in the above program many forms of Sum functions. The first form takes two parameters, the Second form takes three parameters.

Hope, this is a useful post for you.

Thank You

Leave a Comment