What is Object oriented programming
Object oriented programming is a programming paradigm that relies on the concept of class and object. Oops tries to model a real-world system, like In oops, everything is considered as an object where it has certain attributes(property) and method(behavior), for example, if a cat would be considered as an object then his/her height, weights, etc will be considered as it’s attributes and his/her behavior of sleeping too much or chasing rat will be considered as it’s a method. It organizes our program to make it simpler by grouping the related function and variable together into a unit called an object.
Before we learn more about oops, let’s first learn what was missing in our previous programming paradigm
Previously before oops we had a procedural programming paradigm concept, where we divides our program into a set of functions which operates on the data stored in a bunch of variables. It looks very simple and good right! But the problem was, for every single task you create a function and as your program grows it will end up with lots of function, which becomes hard to keep track of and often you will find yourself copying and pasting lines of code, over and over again, if you make changes to one function then several other will break that’s what we call spaghetti code there is so much interdependence between all these functions it becomes problematic.
What are Class and object?
As we discussed earlier oops relies on the concept of class and object, so let’s see what it is
Object is an instance of anything you can think of like a dog, cat, etc. It is the basic unit of oops
Class is like a blueprint or prototype, from which we can create lots of objects. It holds the value of an object’s attributes(properties, like a man height/weight) and method(behavior, like eating/sleeping) for example Apple or any company first creates a blueprint of their product in which they describe what its attributes are like height, weight, quality of the camera, battery type, etc. And what their methods are like, how they will perform, what they will do etc. After that, they start manufacturing it
Also read- What is the difference between libraries vs Framework
What are the Four Pillar of OOPS
There four pillar of object oriented programming are
As the name suggests, inheritance is all about acquiring some or all properties in one object from another object. What basically happens is suppose you created a class “Dog” and you gave it some attributes like the height, weight, number of legs, tail, etc and you also gave some method like eating, sleeping, barking, jumping, wagging tail, etc. And after that you want to add one more class “Cat” now if you notice it has also all the attributes and methods in common with the “Dog” class so instead of recreating all attributes and method separately to “Cat” and “Dog” class, we create a superclass(a base or parent class) “Pet”, where we add the common features generally a pet has in common and then we add a subclass(a derived or child class) which will inherit those attributes and method from superclass in addition to that we can also add some separate new attributes/method to specific subclass like we can add bark method to dog class.
Benefits of inheritance is that it provides reusability of code, we can use the fields and methods of the exciting classes without rewriting it all again.
In Encapsulation a class can maintain a private state means, any other object of different class can’t access it’s attributes or it’s method unless it’s explicitly allowed. It describes the idea of bundling data and methods that work on the data within the same unit.
For example the signal a traffic light shares with the outside world, using different color light to indicate stop/go are public interfaces, while the internal working of traffic light is hidden which is a private interface. Drivers require the traffic light signal to know if they should stop or go. However if the traffic light internal working is exposed to the drivers then that would just confuse the drivers. So, it’s better to hide the irrelevant information which can be achieved by encapsulation.
Data Abstraction is an extension of encapsulation. It is the implementation of hiding the complexity from the users. For example, in order to drive a car, a user doesn’t need to know how his car is working, like when is changing the gear he doesn’t need to know how it is working. In other words, data abstraction is the process of identifying the information which will be irrelevant to the user if displayed
Difference between Encapsulation and Abstraction
Polymorphism is made of two words poly which means many and morph which means forms so it means many forms. It allows you to perform single action in different ways. For example your phone which is a calculator, camera, radio and music player at the same time. So you can see the same device can perform different tasks in different situations.
In simple terms polymorphism means creating a objects to share behavior which allow the same method to execute different behavior.