Object-Oriented Programming or OOPs is a programming paradigm that revolves around the concept of object, which contains properties and methods.
It combines a group of related attributes and behaviour within a single unit named class, that enhances program design structure and code readability.
Further, it also resolves drawbacks of Procedural programming i.e code complexity, unusable code.
Problem with Procedural Programming?
Procedural Programming follows top-down approach, meaning a program is viewed as a series of sequential steps.
In procedural programming, a program is divided into various procedures or functions which operate on data, the issue with this approach is, when the program grows larger, the code redundancy, maintainability, and complexity increases.
Both procedural and object-oriented are imperative programming.
Object-Oriented programming focuses on binding attributes and behavior of a real-world entity represented using an object and supports features like abstraction, encapsulation, inheritance, and polymorphism. These are the following OOPs features.
Other OOPs Features:
Dynamic Binding
Message Passing
Here are the five main features of OOPs
Classes:
A class is the user-defined data type and the main building block of object-oriented programming.
It is an identifiable entity that can have some descriptive properties. It is a user-defined data type that holds data members and member functions in a single unit. It is like a blueprint of an object.
For example, consider an entity “Laptop” , what attributes, you can think of? RAM, OS, memory, manufacturer name, model name and so on.
public class Laptop
{
String modelName;
String manufacturerName;
String ram;
String memory;
String os;
void boot(){
}
}
The above code represents, how a laptop’s attributes and its behavior are put together in a single place.
Objects:
Objects are like a signature of a class. An object is an instance of a class without an object, no memory is allocated to a class’s data members or member function.
With an object of a class, we can access the data members and member functions that can be accessed (as per the private, public, protected accessibility scope).
An object represents a real-world entity, having a set of attributes and behavior. However, an object can’t be simply declared as same as primitive types.
Continuing Laptop’s example, we can define multiple Laptop objects and each object would get the same attributes and behavior declared in the respective class.
Laptop obj = new Laptop();
Laptop obj1 = new Laptop();
Laptop obj2 = new Laptop();
Initializing fields of a single object .
obj.modelName = " Pavilion 15";
obj.manufacturerName = "HP";
obj.ram = "4 GB";
obj.memory = "1 TB";
obj.os = "Windows";
Inheritance:
The ability to inherit the properties of one class to another, or inherit the properties from a base class to an inherited class is known as the concept of Inheritance.
With the help of inheritance, we can use the data members and member functions of a class to another.
It enables a class to acquire or get the properties from another class
It makes code reusable
It makes easier to add new features or methods to a class
It provides an overriding feature which allows a child class to have a specific implementation of a method defined in the parent class
A class that is inherited by other classes is termed as super class or parent class or base class, whereas a class that extends another class is termed as sub-class or child class
Features of Inheritance :
Code Reusability
Ease to add new feature
Overriding
Inheritance can be classified into majorly 5 types : Single Inheritance, Multilevel inheritance, Hierarchical Inheritance, Multiple Inheritance and Hybrid Inheritance
Syntax :
class Laptop extends Electronics
{
}
We can also control or check the data members and member functions to be accessed in the child classes by the means of access specifiers, types of inheritance, access specifiers, and their respective access will be discussed in later articles.
Polymorphism:
Polymorphism is best defined in one statement i.e., “Polymorphism means one name many forms”.
It can be best explained with an example with a comparison to C language; in C language there was one limitation that we can not use the already used function name, but C++ provides us a new feature of Polymorphism, by which we can use the same function name again and again with different signatures.
The in-depth topic of Polymorphism will be discussed in later articles.
Data Abstraction and Encapsulation:
Data Abstraction means hiding the background details and provide only essential details. one of the most common examples regarding this feature is Television, in television we control it with help of a remote and, know its external or the features to be used most whereas we don’t know the internal working of Television.
Encapsulation means binding up data under a single unit. A class is one of the main implementations of this concept. It can be viewed as a wrapper that restricts or prevents properties and methods from outside access. With the help of access modifiers such as private, protected, and public keywords, one can control the access of data members and member functions
Other OOPs Features:
Dynamic Binding:
Dynamic Binding which is also known as Late binding or run-time binding, is a process of executing the part of the code at runtime.
Sometimes we need to access some part at the runtime if we need then we can use this approach. We use virtual functions to achieve Dynamic Binding.
Message Passing:
In this, objects pass the message to each other in order to contact each other. Like when we want 2 or multiple objects to contact each other it is possible with the OOP.
Frequently Asked Questions
Q1. What is an object in OOPs?
An object can be viewed as a real-world entity which has attributes and behaviour. For example, a person, it can have attributes like name, age, gender and behaviour such as talking and walking. These properties are put together within a single unit named class. Therefore, an instance of a class is known as an object.
Q2. Which feature of oops is described as the reusability of code?
Inheritance is the feature of OOPs that describes the reusability of code. It provides the ability to inherit attributes and behaviours from one class to another class. A child class can access and use methods and fields of the parent class which leads to code reusability.
Q3. Which feature of oop is illustrated by function overloading?
Polymorphism is the feature of OOPs that is illustrated by function overloading or method overloading. Method overloading signifies a method having the same name can exhibit multiple functionalities based on its parameters.
Q4. Which two features of oops are the same?
Encapsulation and Abstraction are two features of OOPs that are the same. Encapsulation can manage accessibility and hide the attributes and behaviour of an object. In same way, Abstraction represents only essential data to a user.
Q5. What are the basic principles of OOPs?
Encapsulation, Data Abstraction, Polymorphism and Inheritance are 4 basic principles of Object-Oriented Programming. They are also known as the four pillars of OOPs.