What is Object Oriented Programming
Object-Oriented Programming (OOP) is a programming model that is based on the concept of classes and objects. As opposed to procedural programming where the focus is on writing procedures or functions that perform operations on the data, in object-oriented programming the focus is on the creations of objects which contain both data and functions together.
Object-Oriented Programming works on these terms class, object, properties, methods and Inheritance. To understand all the terms and oops concept we will take a example of robots.
Suppose a robots manufacturing company want to built a new robot. So they will start with a blueprint on paper which will have the following things mentioned on it.
- characteristic of that robots (Number of camera, sensor, arms and wheels)
- List of Functions that robots perform and how they will perform with help of what resource ( camera, sensor, arms and wheels )
Robots blueprint is class, characteristic of that robots is class properties, list of function that robots perform and how they will perform is method of class. Robot built using the blueprint is object of that class. And when we use the old blueprint to built a advance robot that is called Inheritance.
Advantages of OOPs
- Real world programming
- Easier to maintain
- Reusable of code
- Information hiding
- Programmers are able to reach their goals faster.
Fundamental OOP concepts
- Inheritance – Inheritance is an OOPS concept in which one object acquires the properties and behaviors of the parent object. It’s creating a parent-child relationship between two classes.
- Polymorphism – Polymorphism is the ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes. For example, given a base class shape, polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the area method to it will return the correct results.
- Abstraction – Abstraction means, showcasing only the required things to the outside world while hiding the details. Its main goal is to handle complexity by hiding unnecessary details from the user. That enables the user to implement more complex logic on top of the provided abstraction without understanding or even thinking about all the hidden complexity. Consider a real life example of a man driving a car. The man only knows that pressing the accelerators will increase the speed of car or applying brakes will stop the car but he does not know about how on pressing accelerator the speed is actually increasing, he does not know about the inner mechanism of the car or the implementation of accelerator, brakes etc in the car. This is what abstraction is.
- Encapsulation – The encapsulation is the process of grouping or wrapping up of data and functions to perform actions on the data into the single unit. The single unit is called a class. Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object. It keeps the data and the code safe from external interference. The main purpose or use of the encapsulation is to provide the security to the data of a class. To make the data secure we need to use private access modifiers that will restrict the access to the data outside the class. In this OOPS concept, the variables of a class are always hidden from other classes. It can only be accessed using the methods of their current class. Consider a real life example of encapsulation, in a company there are different sections like the accounts section, finance section, sales section etc. The finance section handles all the financial transactions and keep records of all the data related to finance. Similarly the sales section handles all the sales related activities and keep records of all the sales. Now there may arise a situation when for some reason an official from finance section needs all the data about sales in a particular month. In this case, he is not allowed to directly access the data of sales section. He will first have to contact some other officer in the sales section and then request him to give the particular data. This is what encapsulation is. Here the data of sales section and the employees that can manipulate them are wrapped under a single name “sales section”.