Object Oriented Programming concepts are used in all the advanced programming languages. There are various advantages of using oops principles in your programming.
It is very important to have the knowledge of object-oriented programming concepts to develop effective and robust programming.
Entire concept of OOPs revolves around the “Object” and its usage.
The main elements in the OOPs are.
- Object
- Class
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
1. Object:
Object represents any real time entity like Student, Employee etc. It is an instance of a “Class”. Each object consists of “Attributes” and “Behaviors”.
Example: “Employee” is an object will have the following
Attributes:
Name, MobileNumber, Salary, Address etc.
Behavior:
Office login, Task completion, Task submission, Attend meeting, Office logout etc.
Each attribute will carry the data, and that data will be manipulated by the Behavior (actions).
2. Class:
Class is the template of Attributes and Behaviors. The class can only be accessed by creating an instance of it, i.e “object”. Using object only class can be accessed.
We can say class is a user defined data type, a memory will be created when the object of the class is created.
3. Inheritance:
It is a concept of acquiring the properties of one class by another class.
Inheritance is the most important feature of the object-oriented programming language; it plays an important role in programming.
Example:
for Employee in an organization, we have 2 roles one is SoftwareEngineer and another one is ProjectManager
To define the Attributes and Behavior of the 2 roles we need to create 2 class. but if we notice both classes will have common properties like name, mobile, address (values will be different) and actions like attend office, task completion etc. in this case will be duplicate that both classes will have some common attributes and behaviors.
Now to address this scenario we create 2 classes, one with all the common attributes and behaviors and another one with the extended attributes and behaviors.
E.g. Frist class will be Employee class and it will have all the common attributes and behaviors to SoftwareEngineer and ProjectManager. The second class will have the attributes and behaviors expected to ProjectManager class like ApproveLeave, AssignTask.
Now ProjectManager class will inherit the SoftwareEngineer class by means ProjectManager will have access to the SoftwareEngineer class along with its own attributes and behaviors.
There are several types of Inheritance are there in OOPs
Single Inheritance:
Inheriting one class to another class only one to one inherited class. Single parent and single derived class.
E.g. Class B inheriting Class A
Multilevel Inheritance:
Inheriting one class from one class and that class from another class. Single parent and single derived class and this will be a parent to another derived class.
E.g. Class C inheriting Class B and Class B inheriting Class A
Multiple Inheritance:
Inheriting one class from more than one class. Multiple parent and single derived class.
E.g. Class C inherits both Class B and Class A.
Hierarchical Inheritance:
One class inherited by more than one class. Single parent and Multiple derived classes.
E.g. Class A is inherited by both Class B and Class C.
Hybrid Inheritance:
In this, any classes implemented any of the 2 or more different types of inheritance techniques discussed above.
4. Polymorphism:
It represents multiple forms. In simple terms, we can tell it one object behaving multiple behaviors.
E.g. “Dogs” behave differently with different people.
With the owner It will show one behavior. (Follows the owner commands)
With an outsider it will show one behavior. (Barks, bites)
There are 3 types of polymorphism.
Compile Time
This happens at the compile time, the behavior of the object is identified at the compile time only.
Method Overloading
Operator Overloading
Run Time
This happens at the run time, the behavior of the object is identified at the run time
Virtual Function
5. Abstraction:
It is the process of providing only essential information about the data to the outside world, hiding the background details or implementation. In simple terms tell the world to execute the task without knowing what is actually happening behind the task.
E.g. To start a Car, we do not need to have access to the entire engine. will just need a button to start it.
6. Encapsulation:
It is the mechanism of bundling up data under a single unit. In Encapsulation, the variables or data of a class are hidden from any other class and can be accessed only through any member function of their class in which they are declared.
E.g. Operating the TV with remote control. To change the channel or increase/decrease the volume or to change the color setting we send the remote control to the TV but we don’t have any idea about how it is being executed.
Advantages of Object Oriented Programming (OOPs)
- Makes the development and maintenance process of the project easy and save the time and reduces the effort.
- With Abstraction and Encapsulation, security will be achieved.
- It is very easy to develop reusable components.