Understanding Object Oriented concept
Understanding Object Oriented Programming

Understanding Object Oriented concept

#java #oopsconcepts #corejava

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.
No alt text provided for this image
Class with variables

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

No alt text provided for this image
Methods with specific functionality

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.

No alt text provided for this image
Optimized method

Now that we planned everything in the House. We have to start building it.

No alt text provided for this image
Object Creation

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.

No alt text provided for this image
Assigning varaibles and Calling methods using object

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.

No alt text provided for this image
My friends house with different dimensions

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.

To view or add a comment, sign in

More articles by Shilpa Thota

  • Should I hire Mr. Dispatcher Servlet?

    #springframework #spring #springboot #java #webdevelopment #webdesign 😁 Interviewing Mr. Dispatcher Servlet from…

  • The Dump Truck of Java - Garbage Collector

    Before digging deep into Garbage Collection, there are few terms that we should be familiar with. Memory Management-…

Others also viewed

Explore content categories