What is Object Oriented Programming? | OOPs

Object Oriented Programming or (OOPs) is a programming paradigm based on the concept of (as the name suggests) ‘objects’ which collaborate by sending messages to each other.

Objects and Classes

An object encapsulates data and the methods that operates on that data into a single unit.

A car is a perfect example. It has many attributes – color, size, fuel capacity, passenger seats and so on… You can also do a lot of things with it- start the car, stop the car, drive forward or even reverse it. You can turn on the lights, wipers or you can give an indicator. These are its behaviours that can be represented by methods. A car is an entity that contains both its attributes and methods.

How do we create objects?

We create objects through classes. A class is a place where you can identify the behaviors and properties of an object as they are defined inside them.

A class is therefore a template using which you can create as many objects/instances of itself.

   public class Car //defining a class
    {
        private string Color{ get; set; } //this is an attribute
        public string GetColor(); //this is a method
    };
    Car carObj; //this is an object

Encapsulation

In the above example, carObj is an object consisting of the color attribute and the GetColor method. This is an example of an object showcasing encapsulation.

Encapsulation is a concept that binds together or encapsulates the data and the methods that manipulates that data into a single unit.

The data is usually hidden from other objects so the only way to affect the data is through the object’s methods. This not only is a great way to maintain large projects providing flexibility but also prevent data corruption.

Abstraction

Going back to the car example, when you want to move forward, you will simply press the accelerator – without understanding of the internal workings. How the engine powers the car, how the car is wired up or even how the fuel gives enough power to motorize the car body. You successfully interact with the car in abstraction.

This hiding of data and only revealing/interacting with what’s necessary is also called as data hiding or abstraction.

This way you can only focus on how the objects behave and not what is the core required to make them behave in a certain way.

Abstraction and Encapsulation goes hand in hand. When you encapsulate, you abstract away the information.

Inheritance

Classes share some common characteristics – a vehicle has some common attributes. They all have wheels, they all have seats (seating capacity), they all have a model name. The vehicle (which is a class) also have some common behaviors. They all can start and stop.

The car (class) also has the same attributes and behaviors.

To avoid repetition of code because repetition makes the solution bulky and is also prone to errors – we would like to keep common characteristics in a common class so as to a future change is only made once and we do not miss out on changing at the other places as well, we create a parent class or a base class.

Once this parent class is created, all the other classes can inherit from it thus inheriting all the parent class attributes and methods. This is called inheritance.

Inheritance is the process by which one class acquires the properties and functionalities of another class.

This allows re-usability of code and each child class only has to define the attributes which are unique to himself, leaving the rest of the attributes to the parent class.

    public class Vehicle
    {
        public int NumberOfWheels { get; set; }
        public int SeatingCapacity { get; set; }

        public void Start()
        {
            //start vehicle
        }
    }

    public class Car : Vehicle
    {
        public bool IsAutomatic { get; set; }
    }

Car carObj;

In the above example, we have inherited Vehicle class in Car class.

carObj now contains NumberOfWheels, SeatingCapacity and Name as attributes.

Polymorphism

Polymorphism is a feature that allows us to invoke the same operation on objects of different classes and all of them will behave in their own way.

Below is an example:

public abstract class Vehicle
    {
        public int NumberOfWheels { get; set; }
        public int SeatingCapacity { get; set; }

        public abstract void Start();
    }

    public class Car : Vehicle
    {
        public bool IsAutomatic { get; set; }

        public override void Start()
        {
            // start the car
        }
    }

    public class Motorbike : Vehicle
    {
        public override void Start()
        {
            // start the bike
        }
    }

Note that the parent class – Vehicle and its method Start() is marked as abstract. An abstract class provides a common definition of a base class that multiple derived classes can share.  (In this example the Car and Motorbike class)

By marking Start() as abstract, we have left the implementation up to the child classes. This way we can have a single method perform in different ways thus performing polymorphism.


OOPs is undoubtedly one of the most popular programming paradigm today, creating a base for languages like Java, C++, C# and more. It divides responsibilities and promotes re-usability of code.

Be the first one to be notified of any new post, subscribe to my blog! Please? 🙂

Leave a Reply