Vidushi Dwivedi
BlogsCord
Published in
4 min readMay 9, 2020

--

Java : The OOPs Concept

All computer programs are based on 2 units: functions and data.Thus, a program can be organized around its data or its functions.

When a program is written with the concept, “what is happening”, then it is wrapped around functions and this model can be thought of as “functions(code) acting on data”. Here there are different procedures(functions) that are to be called in a series to get a work done. These procedures can be called from within the other procedures as well. It can be stated as a linear series of procedures that act on data. This concept is known as Process-Oriented Model. Procedural languages like C successfully implement this model. The problem with this approach is that the program becomes complex and lengthy.

Figure 1: Procedural Programming Model (Main function calls the procedures and provide appropriate data to them)

On the other hand, when the program is written with the concept of “who is being affected”, then it is wrapped around data and functions provide a well defined interfaces to that data. This model can be thought of as “data controlling access to code(functions)”. This concept is known as Object-Oriented Model. Many languages like C++ implement this model but Java has OOPs at its very core.

Figure 2: Object Oriented Programming(Objects interact with each other through functions and message passing)

OOPs Concept

OOPs concepts in Java are the basic ideas that make Java a popular Object Oriented Programming. These ideas can be listed as :

1. Abstraction

The first essential element of OOPs is Abstraction. The complexity of a program as seen by the user is controlled by abstraction. For example, while playing a video game, a user does not have to understand the mechanism running behind it. Thus, Abstraction can be defined as the process of hiding the complex details and providing the essential data to the user. It is a concept in which the designer seeks ‘what’ instead of ‘how’.It also let programmers create useful, reusable codes.

2. Encapsulation

Encapsulation can be defined as wrapping up of data and functions into a single unit to keep them safe from outside interference and misuse. It can be thought of as a protective wrapper around the data and the functions so that no other code outside the wrapper can affect the data inside the wrapper arbitrarily. There is a well defined interface that tightly controls the access of the inside code and data from the outside.

In real world, think of an ATM. It has hundreds of bits of information encapsulated within it such as your account details, cash-withdraw information, etc but you can access only one at a time. It never happens that you ask for balance enquiry and the ATM withdraws cash, this is achieved through abstraction. The power of encapsulated code is that everyone knows to how to access and thus can use it without the knowledge of implementation details and without the fear of an unexpected outcome.

3. Inheritance

Inheritance is the property by which one object acquires the properties of another object.

The class that acquires the properties of another class is called as child class or a sub class while the class whose properties are being acquired is called as parent class or super class respectively. It can be thought of in the same way as you have acquired the properties of your parents, like the color of the eyes, hairs, etc.

Inheritance in OOPs is an important property as it supports the code being reused by maintaining the hierarchy.For example, there is class that computes Simple Interest. This class can be inherited by some other class that needs to calculate Simple Interest in some process. Thus the programmer does not have to write the code for calculating the Simple Interest once again and can call appropriate functions to of the parent class to compute it. Thus, a sub class has the features and properties of the parent class with its own unique features and properties as well.

4. Polymorphism

Polymorphism is made up of two words- ‘Poly’ means many and ‘morphism’ means forms. In real life, suppose you are given noodles, so you will choose fork to eat it while if you are given soup, then you will use a spoon for it. Thus, depending on the food item, you will decide the medium to eat it. Similarly, polymorphism is the property by which same code behaves differently for different objects or situations.Method overloading and method overriding are two ways of implementing polymorphism in Java.

In this tutorial, we learned the basics about the procedure-oriented language and object oriented language. We also discussed OOPs concept in brief. In the next tutorial we will go through the concept of Classes and Objects.

--

--