• Home
  • /
  • Blog
  • /
  • 4 Powerful Features of OOPs

4 Powerful Features of OOPs

Powerful features of OOPs

This post is also available in: हिन्दी (Hindi) العربية (Arabic)

Since the release of the first high-level programming language, many different programming languages have evolved. All these programming languages follow a certain approach to writing programs. This is often called a programming paradigm.

A programming paradigm is a concept to which the methodology of a programming language adheres. Paradigms are important because they define a programming language and how it works. There are various programming paradigms or approaches and Object-Oriented Programming (OOPs) is one of the most commonly used paradigms because of the 4 powerful features of OOPs.

What Is Object-Oriented Programming?

As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of these features of OOPs is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.

CodingHero - 4 Powerful Features of OOPs 1 4 Powerful Features of OOPs 952

A feature of objects is that an object’s own procedures can access and often modify the data fields of itself (objects have a notion of this or self). In OOP, computer programs are designed by making them out of objects that interact with one another. OOP languages are diverse, but the most popular ones are class-based, meaning that objects are instances of classes,  which also determine their types.

Some of the most commonly used Object Oriented Programming languages are Python, Java, Go, Ruby, C++.

Why is the concept of OOPs Important?

The Object-Oriented Programming languages have an edge over procedural languages because of the following four features of OOPs:

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

Let’s look at these features and see what they mean and what makes them unique?

Abstraction

The foremost in the list of features of OOPs is Abstraction. Abstraction is the concept of object-oriented programming languages that “shows” only essential attributes and “hides” unnecessary information. The main purpose of abstraction is to hide unnecessary details from the users. Abstraction is selecting data from a larger pool to show only relevant details of the object to the user. It helps in reducing programming complexity and efforts. It is one of the most important concepts of OOPs.

Let’s understand the concept of abstraction by considering an example. Suppose you want to create a banking application and you are asked to collect all the information about your customer. There are chances that you will come up with the following information about the customer:

  • Name
  • Address
  • Contact Number
  • Tax Information
  • Favourite Food
  • Favourite Book
  • Favourite Movie
  • Favourite Actor

But, not all of the above information is required to create a banking application. 

So, you need to select only useful information for your banking application from the above pool of information. Data like name, address, contact number, tax information make sense for a banking application which is an abstraction example in OOPs.

Since we have fetched/selected the customer information from a larger pool, the process is referred to as Abstraction in OOPs.

However, the same information once extracted can be used for a wide range of applications. For example, you can use the same data for a Government database, hospital application, etc., with little or no modification. Hence, it becomes your Master Data. This is an advantage of Abstraction in OOPs.

How Abstraction is Implemented?

You can implement Abstraction in OOPs using Classes. A class helps us to group data members and member functions using the available access specifiers. A Class can decide which data member will be visible to the outside world and which is not.

Access specifiers are the main pillars of implementing abstraction in OOPs. You can use these access specifiers to enforce restrictions on class members. These access specifiers can be 

  • Public: Members declared as public in a class can be accessed from anywhere in the program.
  • Private: Members declared as private in a class can be accessed only from within the class. They are not allowed to be accessed from any part of the code outside the class.
class implementAbstraction
{
    private:
        int a, b;
    public:
        // method to set values of 
        // private members
        void set(int x, int y)
        {
            a = x;
            b = y;
        }
          
        void display()
        {
            cout<<"a = " <<a << endl;
            cout<<"b = " << b << endl;
        }
};
int main() 
{
    implementAbstraction obj;
    obj.set(10, 20);
    obj.display();
    return 0;
}

You can see in the above program we are not allowed to access variables a and b directly, however, you can call the function set() to set the values in a and b and the function display() to display the values a and b.

Advantages of Data Abstraction

  • Helps the user to avoid writing low-level code.
  • Avoids code duplication and increases reusability.
  • Can change the internal implementation of class independently without affecting the user.
  • Helps to increase the security of an application or program as only important details are provided to the user.
  • Helps to increase the security of an application or an application or program as only important details are provided to the user.

Encapsulation

The second in the list of features of OOPs is Encapsulation. In object-oriented computer programming languages, the notion of encapsulation refers to the bundling of data, along with the methods that operate on that data, into a single unit. Many programming languages use encapsulation frequently in the form of classes. A class is a program-code template that allows developers to create an object that has both variables (data) and behaviors (functions or methods). A class is an example of encapsulation in computer science in that it consists of data and methods that have been bundled into a single unit.

Encapsulation may also refer to a mechanism of restricting the direct access to some components of an object, such that users cannot access state values for all of the variables of a particular object. Encapsulation can be used to hide both data members and data functions or methods associated with an instantiated class or object.

What are Classes and Objects in OOPs?

A Class in OOPs is the building block that leads to Object-Oriented programming. It is a user-defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.

For Example: Consider the Class of Cars. There may be many cars with different names and brands but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range, etc. So here, Car is the class, and wheels, speed limits, mileage are their properties or data.

A Class is a user-defined data-type that has data members and member functions.

Data members are the data variables and member functions are the functions used to manipulate these variables and together these data members and member functions define the properties and behavior of the objects in a Class.

In the above example of class Car, the data member will be speed limit, mileage, etc and member functions can apply brakes, increase speed, etc.

An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.

When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects.In the code above, implementAbstration is a class and obj is an object of the class, and the data – a and b and the functions – set and display are bound together.

Advantages of Data Encapsulation

The main advantage of using encapsulation is the security of the data. Benefits of encapsulation include:

  • Encapsulation protects an object from unwanted access by clients.
  • Encapsulation allows access to a level without revealing the complex details below the level.
  • It reduces human errors.
  • Simplifies the maintenance of the application.
  • Makes the application easier to understand.

For the best encapsulation, object data should almost always be restricted to private or protected. If you choose to set the access level to the public, make sure you understand the ramifications of the choice.

Inheritance

The next in the list of features of OOPs is Inheritance. As with the other principles of OOPs, inheritance is meant to optimize the work of programmers. The role that inheritance plays in this optimization is in allowing software engineers to create class hierarchies, where classes and objects inherit properties and behaviours from their parent (or super) class. A class that inherits from a parent (pr super) class is called a subclass or child class and objects that receive properties from a parent through inheritance are referred to as child objects. A big part of the usefulness of inheritance is reusability.

Sub Class: The class that inherits properties from another class is called Sub Class or Derived Class.

Super Class: The class whose properties are inherited by sub-class is called Base Class or Parent Class or Super Class.

Let’s take an example of a doctor and a surgeon. A surgeon is basically a doctor having expertise in surgery. Here the doctor is a parent class and the surgeon is a child class. The class surgeon has all the properties of the class doctor along with some extra properties.

Types of Inheritance

In OOPs, there can be the following types of inheritance:

  • Single Inheritance: In single inheritance, one class extends to another class (one class only). For example, class Dog inherits the properties of class Animal. Here Dog is a child class and Animal is a parent class.
  • Multiple Inheritance: In multiple inheritance, one class extending more than one class.  For example, a class Bat inherits the properties of class Mammal and WingedAnimal. 
  • Multilevel Inheritance: In multilevel inheritance, one class inherits from a derived class. Hence, the derived class becomes the parent class for the new class. For example, a class Puppy inherits the properties of the class Dog which inherits the properties of the class Animal.
  • Hierarchical Inheritance: In hierarchical inheritance, one class is inherited by many subclasses. For example, the class Dog and Cat inherits the class Animal.
  • Hybrid Inheritance: Hybrid inheritance is a combination of single and multiple inheritance.
Powerful features of OOPs
Types of Inheritance

Advantages of Inheritance

  • The main advantage of the inheritance is that it helps in reusability of the code. The codes are defined only once and are used multiple times. 
  • Through inheritance a lot of time and efforts are being saved.
  • It improves the program structure which can be readable.
  • The program structure is short and concise which is more reliable.
  • The codes are easy to debug. Inheritance allows the program to capture the bugs easily.
  • Inheritance makes the application code more flexible to change.
  • Inheritance results in better organization of codes into smaller, simpler and simpler compilation units.

Polymorphism

Polymorphism is the ability of any data to be processed in more than one form. The word itself indicates the meaning as poly means many and morphism means types. Polymorphism is one of the most important concepts of object-oriented programming language. The most common use of polymorphism in OOPs occurs when a parent class reference is used to refer to a child class object. 

Real-life examples of polymorphism, a person at the same time can have different roles to play in life. A woman at the same time is a mother, a wife, an employee, and a daughter. So the same person has to have many features but has to implement each as per the situation and the condition. Polymorphism is considered one of the important features of OOPs.

Polymorphism is the one in the list of features of OOPs is the one that performs different things as per the object’s class, which calls it. With polymorphism, a message is sent to multiple class objects, and every object responds appropriately according to the properties of the class.

Now let’s say we have two subclasses of class Animal – Horse and Cat that extends the class Animal (through inheritance). We can provide the implementation of same method Sound like this:

public class Horse extends Animal{
...
    @Override
    public void sound(){
        System.out.println("Neigh");
    }
}

and

public class Cat extends Animal{
...
    @Override
    public void sound(){
        System.out.println("Meow");
    }
}

Here you can see that although we had the common action for all subclasses sound() but there were different ways to do the same action. This is a perfect example of polymorphism (a feature that allows us to perform a single action in different ways). It would not make any sense to just call the generic sound() method as each Animal has a different sound. Thus we can say that the action this method performs is based on the type of the object.

Advantages of Polymorphism

  • Programmers code can be reused via polymorphism.
  • Supports a single variable name for multiple data types.
  • Reduces coupling between different functionalities.

Object-oriented programming and features of OOPs necessitates planning and thinking about the program’s structure before starting to code and examining how to decompose the requirements into basic, reusable classes that you may utilize to create object instances. Overall, using OOP provides for more reusable data structures and saves time in the long run.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>