It is a style of programming that is centered around objects rather than functions, nothing new it’s around been since 70’s unlike tools and other frameworks, but still it is very relevant today and in future.
There are many other languages which supports object oriented programming such as C#, Java, Python, Ruby, Javascript and more.. There are many popular frameworks those are actually designed with object oriented programming concepts in mind such as angularjs, reactjs.
There are four important concepts in object oriented programming such as
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
Let’s see it in detail why it is used and what for.
- Encapsulation :
In object oriented programming we group related variables and functions that operate on them into objects and this is what we call Encapsulation, if you look at the below example where we have a class, object and function.
Class is very much help full in small and large application it can be reused across whole application and we don’t have to worry about confusions and calling the normal function with parameters again and again.
If you notice the getWage function inside the class it does not have any parameters because all the parameters are modeled as properties of this object all these properties and getWage function they are highly related so that they part of one unit, when you write your function in object oriented way your functions end up having fewer and fewer parameters which is easier to use and to maintain.
2. Abstraction :
Think of a mobile phone as an object which has a complex logic board inside and if user tap’s outside screen to make a call or to play a video to interact with user don’t care what happens inside within it, all the complexity is hidden from the user this is abstraction as an example.
So let’s use the same example in our objects to hide some properties and methods which the user does not want now but need it in near future whenever it is required, in the below example image you will see that class has been created as a person within which we have objects and function.
Now only the user wants to required or what ever he / she wants to see just like in the example
firstName, LastName and age is what only required by the user rest of the parameters are not required just in case in future if you change or delete the inner private methods none of the changes will effect outside this class as we don’t have any code which touches these methods outside of their content object, so with the help of this impact of change will be reduced.
3. Inheritance :