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?
Object-oriented programming, or OOPs, is a programming model which breaks down a problem in terms of classes and objects. OOPs allows the creation of several instances of a class called objects, hence facilitating code reuse. Some object-oriented programming languages are C++, Java, Javascript,Python, etc.
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.
Top Features Of Object Oriented Programming
The main features of object oriented programming are as follows:
Classes
Objects
Abstraction
Polymorphism
Inheritance
Encapsulation
In the upcoming sections, let’s see each object-oriented programming feature in detail.
Classes and Objects
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.
Example:
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.
Example:
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.
Abstraction
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
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.
Example:
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.
Polymorphism
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.
Method Overloading
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.
Example:
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
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.
Example:
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
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.
Example:
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.
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
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.
Frequently Asked Questions
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 are the main features and principles of OOPs?
The main features and principles of Object-Oriented Programming (OOP) include encapsulation, inheritance, polymorphism, and abstraction. These concepts promote code reusability, modularity, and ease of maintenance in software development.
What is the full form of OOPs?
The full form of OOPs is Object-Oriented Programming. It's a programming paradigm that focuses on organizing code into objects, which encapsulate data and behavior, promoting modularity and reusability in software development.
What is an object in OOPs?
In Object-Oriented Programming (OOP), an object is an instance of a class that encapsulates data (attributes or properties) and behavior (methods or functions). Objects represent real-world entities and are the building blocks of OOP systems.
Conclusion
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.