Object Oriented programming started a new era of programming. Its re-defined the way you can write and control your code. Let us now understand the basics of OOP and how it can change your coding style.
Object oriented programming
The concept of OOP is based on the phenomenon of implementing the real life entities into the programming or say virtual entities. For example, how would you represent a car in procedural language. It seems like impossible but in the OOP you can do that easily.
Basically, OOP has two components:
State - which we represented by the fields or commonly known as variables.
Behaviour - Which we commonly know as functions or procedures.
In our car example we have following are the states:
type(hatchback, suv, sedan), color(red, blue, black), speed, gear and the behaviour is how you would change the state
a. Increase the speed of car
b. Change the gear of the car
Now that we know what does OOP means let us have a look at what power oop gives to our programming.
1. Abstraction:Abstraction means to provide what is needed and hide the rest. To be more precise let us look back at our Car. Do we really care about how the engine works or how many nut-bolts, or valves it contains. the answer is NO ! all we care about it that its running. we can control it. The same goes with the softwares. We can control how a user will interact with the data. The data is core of any software and we need to protect it, so priority shall be the protection of data by not letting anyone access it directly. That is what the role of a method is.
2. Encapsulation: Many people confuse the abstraction and encapsulation to be exact the same. But there is a slight difference which needs to be identified. Now, encapsulation means to break down the whole lot of bigger thing into small pieces so that it is manageable. Of course it means we are hiding the data but we are keeping things manageable. for example in car, there are lots of things, which we don’t care about, like where the gear handles the speed, or where the steering controls the wheels, but logically this makes sense that these two have different tasks to do and hance its good to keep their functionality different. Same goes with the software. If one part is interaction with database and one with user its good to encapsulate these in two parts rather than mixing up.
3. Polymorphism: Yes ! it is old friendly poly(multi)+morphism(forms). The oop gives you power to define multiple forms for the same thing. For example, The Car could be auto-transmission or manual. similarly it could be petrol version or diesel version. Similarly in the program if you want to search something from the database, you can make a function which will search for name, or first and last name. so OOP gives you power to make this happen with the same function name rather than different name.
4. Inheritance: Inheritance takes in the general meaning in oop too. that means you can inherit the property from some code into other part of code, without having to rewrite the whole code again. In this scenario we call the main code parent or super(in java) and in which we inherit its property, the sub. In this way you don’t have to write the code again and again.
5. Code maintenance: By maintenance I mean you can group and organise the code in a proper manner. for example, if you are writing code for UI it can be separated and put in a different directory, and DB related code in different directory.
This is how the oop is beneficial to you. if you are starting your career in programming world you’ll have many encounters with these concepts in languages like, C++, Java, Scala etc.