Object Oriented Programming(OOP) is one of the most popular programming paradigms that designs software in terms of objects containing data and code. Most programmers use OOPs to solve problems and design software applications as it facilitates code reuse and troubleshooting.
In this article, we will learn the key features of object-oriented programming, its advantages and disadvantages, and some examples.
What is Object Oriented Programming?
The four main pillars or features of object-oriented programming include Abstraction, Polymorphism, Inheritance, and Encapsulation, or you can learn it as A PIE to recall all of them easily.
Let’s learn the important features of object-oriented programming and examples in the next section.
Features Of Object Oriented Programming
The main features of object oriented programming are as follows:
In the upcoming sections, let’s see each object-oriented programming feature in detail.
Classes and Objects in OOPs
A class is a template that consists of the data members or variables and functions and defines the properties and methods for a group of objects.
The compiler does not allocate memory whenever you define a class.
You can define a class called Vehicle. Its data fields can be vehicle_name, model_number, color, date_of_manufacture, etc.
An object is nothing but an instance of a class. Each object has its values for the different properties present in its class. The compiler allocates memory for each object.
The different objects of the class Vehicle can be Car, Bike, Bicycle, etc. Each of them will have its values for the fields like color, model_number, etc.
The literal meaning of abstraction is to remove some characteristics from something to reduce it to a smaller set. Similarly, Object Oriented Programming abstraction exposes only the essential information of an object to the user and hides the other details.
In real life, like when you toggle a switch, it simply turns on or off the lights. Here, we only know the functionality of the switch, but we don’t know its internal implementation, like how it works.
How to implement abstraction?
You can implement abstraction using classes that group the data members and function together. Inside classes, you can choose the access specifiers for its members to control how they are visible to the outside world. We can also create header files containing the implementations of all the necessary functions. So, you can include the header file and call these functions without getting into their implementation.
Advantages of Abstraction
The advantages of abstraction are as follows:
- It enables code reuse by avoiding code duplication.
- It enhances software security by making only necessary information available to the users and hiding the complex ones.
Inheritance is one of the most important features of object oriented programming. It allows a class to inherit the properties and methods of another class called the parent class, the base class, or the super-class.
The class that inherits is called the child class or sub-class.
It helps to avoid duplication of codes by allowing code reuse as you need not define the same methods and properties present in a super-class in the sub-classes again. The sub-class can simply inherit them.
You can have a parent class called “Shape” and other classes like Square, Circle, Rectangle, etc. Since all these are also shapes, they will have all the properties of a shape so that they can inherit the class Shape.
The word polymorphism means to have many forms. So, by using polymorphism, you can add different meanings to a single component.
There are two types of polymorphism:
- Run-time polymorphism
- Compile-time polymorphism
Let's see each type in the next section.
Methods overloading is a type of compile-time polymorphism using which you can define various functions with the same name but different numbers of arguments. The function call is resolved at compile time, so it's a type of compile-time polymorphism. Here resolution of the function call implies binding to the correct function definition depending on the arguments passed in the function call.
You can create a function “add”. Now, when you pass two integers to this function, it will return their sum, while on passing two strings, it will return their concatenation.
So, the same function acts differently depending on the input data type.
Method Overriding is a type of run-time polymorphism. It allows overriding a parent class’s method by a child class. Overriding means that a child class provides a new implementation of the same method it inherits from the parent class.
These function calls are resolved at run-time, so it's a type of runtime polymorphism.
You can have a parent class called “Shape” with a method named “findArea” that calculates and returns the area of the shape. Several sub-classes inherit from the “Shape,” like Square, Circle, Rectangle, etc. Each of them will define the function “findArea” in its way, thus overriding the function.
Encapsulation means enclosing the data/variables and the methods for manipulating the data into a single entity called a class. It helps to hide the internal implementation of the functions and state of the variables, promoting abstraction.
You can have some private variables in a class that you can't access outside the class for security reasons. Now, to read or change the value of this variable, you can define public functions in the class which will perform the read or writes operations.
Know What is Object in OOPs here in detail.
Other Oops Features:
Dynamic binding takes place during run time based on the type of object. Since it is delayed till the run time, it is also called late binding or runtime binding. When the compiler cannot determine all the information required to resolve a function call during compile time, these function calls are not bound until run time.
Message passing refers to the process of passing a message, or data, between different objects or components in a program. This can be done in many ways, such as function calls, events, or inter-process communication. The specific implementation of message passing will depend on the program's design and the system's needs.
Click on the following link to read further: Features of C Language
Advantages of Object Oriented Programming
The advantages of object oriented programming are as follows:
- It makes troubleshooting easier and faster by making the code modular. So, you can look at the particular class or method whenever an error occurs instead of checking the entire code.
- It allows code reuse by inheritance.
- It enables flexibility through polymorphism as one function or object can adapt to several forms according to the requirement.
- It allows you to solve a problem efficiently by breaking a huge problem into smaller manageable parts like classes and objects.
Check out this article - Compile Time Polymorphism
Disadvantages of Object Oriented Programming
The disadvantages of object oriented programming are as follows:
- It has a steep learning curve because breaking down a problem into simple components requires long-term thinking, which comes with experience.
- Generally, the code becomes larger in object-oriented programming compared to procedural programming.
Frequently Asked Questions
What is the main use of object-oriented programming?
Object-oriented programming is ultimately about taking a huge problem and breaking it down into solvable chunks.
What are the main features of object-oriented programming?
The main features of object-oriented programming are Class, Object, Inheritance, Encapsulation, and Abstraction Polymorphism.These features make the code flexible , extensible, reusable and easy to understand.
Which two features of the Oops are same?
Encapsulation and Abstraction are similar concepts in OOPs, Encapsulation hides the features of the object and binds all the properties inside a single class. Abstraction is a feature that shows limited data to the user.
What cannot be used for polymorphism?
The polymorphism feature is illustrated by function overloading. Polymorphism allows one function name to work for different parameters.The function call is resolved at compile time, so it's a type of compile-time polymorphism.
In this article, we learned what is OOPs, and the features of object oriented programming like abstraction, polymorphism, inheritance, and encapsulation, along with real-life examples. We also saw the advantages and disadvantages of object oriented programming.
We hope that this blog has helped you enhance your knowledge regarding the features of object oriented programming.
Check out these useful blogs on -
- Classes and Objects
- Understanding The Difference Between Structure And Class In C++
- Constructors and Destructors
Difference Between Procedural and Object Oriented Programming