Object-Oriented Programming (OOP) is one of the programming paradigm or programming style. Popular programming languages like Python, Java, C++, etc. follows an OOP paradigm.
OOP is not the only style of programming. If you know C language and have done some coding in C, then you have followed Procedure Oriented Programming (POP) paradigm.
Procedural programming is suitable for small projects; as size increases, it's complicated and hard to manage.
We can program in both paradigms; OOP provides some additional features over POP like encapsulation, inheritance, polymorphism, etc.
Data is a valuable asset to any organization; So it's important to give security to it.
The POP approach is less concern about data security. OOP's central focus is on data security.
Class and Object :
A class is a blueprint or prototype for creating objects.
A class defines attributes and functions for an object.
An object is an instance of a class.
If you take "mango" as an object; then "fruit" is a class.
Similarly, if you take "tiger" as an object; then "animal" is a class.
A class is also known as an Abstract Data Type (ADT). You will understand why it is called ADT by the following example.
If I told you to draw a fruit; you will ask me which fruit? , because you know there are a variety of fruits with different shape, color and taste. but if I told you to draw "mango" then you will be able to draw "mango" without confusion.
You can draw a "mango" because of its a real thing whereas "fruit" is just a blueprint or abstract thing.
Encapsulation :
Encapsulation is a mechanism for wrapping up the data and code into a single unit (generally into class).
Encapsulation provides security to your code.
Inheritance :
In inheritance, one class acquires the properties of another class.
With inheritance, we can extend the class with additional attributes and methods.
A class from which new class is created is known as the superclass or parent class, and the new class is known as subclass or child class.
Child class contains all the data & methods of a parent class as well as its own.
Consider a parent class "mobile-phone" with attributes name, model, weight, speaker; and with functions makeCall(), receiveCall(), sendSMS(), etc. If you extend the class "mobile-phone" with the class named "smartphone" then the class "smartphone" will have all the attributes and methods of the class "mobile-phone" as well as its own attributes camera, gyroscope-sensor and functions capturePhoto(), browseInternet(), etc.
Polymorphism :
"Poly" means many and "morph" means form. The ability of an object, variable, or function to take more than one form is known as polymorphism.
Consider two methods calculateArea(radius) and calculateArea(length, width). If we provide a radius; the first function gives an area of a circle. If we provide length and width; the second function gives an area of a rectangle.
In the above example, we used the same function name with a different prototype.
There are two types of polymorphism :
- Compile-time polymorphism
- Run-time polymorphism
