Pillars Of OOP/Overview Of OOP

Object Oriented Programming

A programming model that is mainly organized around objects is called Object object-oriented programming or Programming where the main focus of the programmer while modeling any problem is objects. (The name OOP indicates the same).

What is Class?

  1. Class is a template/blueprint for the Objects. It defines object behavior (in the form of class methods) and object attributes (in the form of properties or class data members).
  2. A software system may consist of many classes (A real-time system may have a thousand or more classes). Working with so many classes is not so easy, so we require some way so that they can be managed properly or efficiently.
  3. In order to manage so many classes and to reduce the complexity, system designers use several techniques that can be grouped under four main concepts Encapsulation, Abstraction, Inheritance, and Polymorphism.
  4. These concepts are the four main gods of the OOP world and in software terms, they are called the four main Object Oriented Programming (OOP) Concepts (or four fundamentals of OOP or four pillars of OOP).
  5. In the following paragraphs for each pillar, I have given real-life examples. The advantage, in most of the interviews I faced that question (what is real real-life example of abstraction) and it will make the picture of the corresponding very clear.

1. What Is Encapsulation?

Encapsulation is a process of binding data members (variables, properties) and member functions (methods) together. In object-oriented programming language we achieve encapsulation through Class.

Real-Life Example Of Encapsulation is the Capsule. Capsule binds all chemical contents required for curing a specific disease together just like the class which binds data members and member functions.

Benefits/Advantages Of Using Encapsulation
 

Encapsulation As Information Hiding Mechanism

  1. As already discussed, encapsulation is a process of binding data members (variables, properties) and member functions (methods) together. Due to this approach, the contents (or data members) of the object are generally hidden from the outside world (By simply making the data members private) which is nothing but information hiding.
  2. Typically, only the object's own methods can directly inspect or manipulate its fields. So the outside world only knows what a class does (Through the member functions) but doesn't know what are its data members and how that class does a specific task.
  3. Hiding the internals of the object or allowing access to it through only a member function protects object integrity by preventing users from setting the internal data or the component in such a way that may lead to an invalid or inconsistent state of the object.

Implementation In Programming Language

public class Account
{
    private double balance;
    
    public double GetBalance()
    {
        return balance;
    }
}

// Imagine main as outside world
public static void Main()
{
    Account obj = new Account();
    double currentBalance = obj.GetBalance();

    // The following thing is not possible since that data member is private
    obj.balance = 20000000000;
}

Imagine what disaster could have happened if the balance (data member) had been made public. We could have been the richest person in the world.

2. What Is Abstraction?

Abstraction is the process of showing only essential/necessary features of an entity/object to the outside world and hiding the other irrelevant information.

In a programming language, we achieve abstraction through public and private access modifiers and a class. So in a class make things (features) that we want to show as public and things that are irrelevant make them as private so they won't be available to the outside world.

Real Life Example Of Abstraction

A real-life example of Abstraction could be the gears of a bike. Do we know what happens inside the engine when we change the gear? The answer is No (what happens inside the engine when we change the gear is irrelevant information from the user's perspective so we can hide that information). What is important from the user's perspective is whether the gear has been changed or not. (This is an essential/necessary feature that must be shown to the user).

So the abstraction says only to expose the details that really matter from the user's perspective and hide the other details.

Benefits/Advantages Of Using Abstraction

The advantage of Abstraction is, it reduces the complexity of end users since to the end user we have shown only things that are necessary to him and not shown the unnecessary things. Imagine what would have happened when we told the bike rider things happening inside the engine (i.e. irrelevant information) when he changed the gear. So it would have increased the complexity of bike riders.

Implementation In Programming Language

Imagine a car. Now let's think in terms of a car rider or a person who is riding a car. So to drive a car what a car rider should know and what not.

Necessary things

  • Brakes
  • Gear
  • Steering

Unnecessary things

  • Exhaust System
  • What happens when he changes the Gear of the car?
  • Silencer

Here I have written the code snippet in C# to explain the above-mentioned things.

class Car  
{  
    public string Brakes { get; set; }  
    // Gear is exposed using the public access modifier.  
    public string Gear { get; set; }  
    public string Steering { get; set; }  
  
    private void Exhaust_System()  
    {  
        // Implementation  
    }  
    
    // Hidden using the private access modifier  
    private void What_happen_we_he_change_the_Gear_of_car()  
    {  
        // Implementation  
    }  
    
    private void Silencer()  
    {  
        // Implementation  
    }  
}  

Note. Abstraction and abstract class are two different things so they are not much related. Do not confuse by comparing those two. Consider them as independent.

3. What Is Inheritance?

The process of creating a new class by extending the existing class is called inheritance or the process of inheriting the features of the base class is called as inheritance.

The existing class is called the base class and the new class which is created from it is called the derived class.

Real Life Example Of Inheritance

We inherit some of our features (maybe body color, nose shape, height of body, etc) from Mom and Dad.

Benefits/Advantages Of Using Inheritance

The most important advantage of inheritance is code re-usability. In Inheritance the derived class possesses all attributes/properties and functions of the base class, this is where code re-usability comes into the picture. So no need to write the same function and attributes of the base class in the derived class.

Note. Private members of the base class also get inherited in the derived class but they are not accessible in the derived class.

Implementation Of Inheritance In Programming Language

// Suppose initially I wrote a base class Shape as
class Shape  
{  
    public int width;  
    public int height;  
    
    public void SetWidth(int w)  
    {  
        width = w;  
    }  
    
    public void SetHeight(int h)  
    {  
        height = h;  
    }  
}  
  
// Derived class Rectangle  
class Rectangle : Shape  
{  
    public int GetArea()  
    {  
        return width * height;  
    }  
}  

Like rectangles, we may have parallelograms, and rhombus so there is no need to declare width and height each time, just inherit those classes (parallelogram, rhombus) from the Shape class. This is how code reusability comes into the picture.

class Tester  
{  
    static void Main()  
    {  
        // Derived class object  
        Rectangle obj = new Rectangle();  
        obj.SetWidth(5);  
        obj.SetHeight(10);  
        Console.WriteLine("Area of rectangle is {0}", obj.GetArea());  
        Console.Read();  
    }  
}  

What Is Polymorphism?

Poly means many and Morph means forms. Polymorphism is the process in which an object or function takes different forms.

Real-Life Example Of Polymorphism

A real-life example of Polymorphism is a mobile phone. It is a single object but it can be used for making calls, listening to music, sending mail, taking pictures, etc (different forms).

Implementation Of Polymorphism In Programming Language

We can implement polymorphism using method overloading, method overriding, etc.

// Implementation of polymorphism using method overloading
class Base  
{  
    public int Add(int num1, int num2)  
    {  
        return (num1 + num2);  
    }  
    
    public int Add(double num1, double num2)  
    {  
        return (int)(num1 + num2);  
    }   
}  

class Program  
{  
    public static void Main()  
    {  
        Base obj = new Base();  
        Console.WriteLine("Addition of two integer numbers is " + obj.Add(4, 5));  
        Console.WriteLine("Addition of two float numbers is " + obj.Add(6.78, 5.23));  
    }  
}

So what we observe is function Add takes two forms. The first form is for adding integer numbers and the second form is for adding float numbers.

Advantages/Benefits Of Using Polymorphism

The main advantage of polymorphism is, that it makes the life of the end-user easy since instead of having two methods (as shown in the above example) AddInt for adding an integer and AddFloat for adding the float we have only one method Add. So end user have to remember only the Add method and not AddInt and AddFloat.


Similar Articles