Understanding Object Oriented concept
In Layman’s terms, who are new to the programming world, the term object oriented programming sounds little complicated but it is very easily understood if we compare the same with real-life examples.
Let us first understand what is a class?
To construct a house we first go to a civil engineer and draw a blueprint of the building. Based on the blueprint, you calculate length and breadth or volume occupied by each room. We divide house into bedroom, kitchen, bathroom etc., and calculate space occupied by each resource and then starts building.
Same example can be imagined as a program using a class.
Class is a blueprint of the object that is being built.
Let’ say we created class “House”. Engineer gives few requirements for the house like the house can be built with certain length, breadth, height. These are nothing but variables which is used by rooms to plan.
Let's create a method called Bed Room in my house. It has some specific requirements like it should not be more than 20% of my entire volume. Those requirements are now separated and calculated with in my Bedroom method and returns me perfect bedroom. Same goes with the other rooms and balcony
This improves modularity of my program by separating the logic for each room. Now let us optimize it further, In each method we have repetition of code where same logic is applied for each room.
Recommended by LinkedIn
Now that we planned everything in the House. We have to start building it.
This is where the object comes into picture. We can use the blueprint class and create object for it using `new` keyword.
Now that JVM knows what is the data type of the variables and how much space is required for computations for the class, when an object is created the space is allocated to the house object when it encounters new keyword.
Object is the instance of a class
As the object or space is ready. we can start building home by assigning the dimensions of the building in variables, calling the methods like constructRoom which returns me the bedroom with occupied volume. The method access the variables in the space that the object occupied.
For instance, My friend also liked my house and asked for the blueprint and he uses the same blueprint and constructs the house at some other location which is different from my location but uses the same blue print.
He modifies the dimensions based on his requirement but uses the same blue print. Here, we are creating the second object of the same class in some different memory space. When we call methods using house2 object it returns the dimensions and specifications of the house2.
As you can observe, all the computations and the way the program is written is around the class and object in java rather than functions and logic. Hence it is object oriented programming.