Java OOPs Concetps | Object Oriented Programming in Java

encapsulation in java Object Oriented Programming Concepts in Java

Object Oriented Programming concept is the main and widely used concept in Java programming language. Java is partially Object Oriented Programming language because it supports primitive data types such as int, char, float etc.

In this tutorial, we will discuss about Object Oriented Programming concept in Java. We will cover each and every aspect of OOPs in java in this post.

What is Object Oriented Programming

The first question that comes to our mind : What actually OOPs is ? Why are we using this OOPs concept in Java? What is the use of this concept ?

To understand OOPs better, you can refer the explanation provided  by Steve Jobs :  Steve Jobs in 1994: The Rolling Stone Interview.

Would you explain, in simple terms, exactly what object-oriented software is?
Steve : Objects are like people. They’re living, breathing things that have knowledge inside them about how to do things and have memory inside them so they can remember things. And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction, like we’re doing right here.

Here’s an example: If I’m your laundry object, you can give me your dirty clothes and send me a message that says, “Can you get my clothes laundered, please.” I happen to know where the best laundry place in San Francisco is. And I speak English, and I have dollars in my pockets. So I go out and hail a taxicab and tell the driver to take me to this place in San Francisco. I go get your clothes laundered, I jump back in the cab, I get back here. I give you your clean clothes and say, “Here are your clean clothes.”

You have no idea how I did that. You have no knowledge of the laundry place. Maybe you speak French, and you can’t even hail a taxi. You can’t pay for one, you don’t have dollars in your pocket. Yet I knew how to do all of that. And you didn’t have to know any of it. All that complexity was hidden inside of me, and we were able to interact at a very high level of abstraction. That’s what objects are. They encapsulate complexity, and the interfaces to that complexity are high level.


In Simple Terms

Object Oriented Programming is a methodology or programming language model to design and develop different components of a software. Everything in OOPs is grouped as “Objects”.  All objects have their states (called data) and related behavior (called methods).

What are the concepts of OOPs

Now we will discuss different concepts included in OOPs model as below :

   1. Inheritance
   2. Encapsulation
   3. Abstraction
   4. Polymorphism

Inheritance

Before we directly go to the definition, let’s understand inheritance with simple real world example.

“Let’s take an example of a car. In day to day life, we see different cars of different models. They are different in structure, speed and facilities. But what we call them is important, we call them “Car” whether it is audi, BMW, maruti etc. They all are “Car”. So, Car is the parent of all the cars available in the market. Every Car has engine, tyres, steering, break etc and different models in the market are inheriting the properties of Car like they all have engine, tyres, gear, steering.”

inheritance in java
inheritance in java


Inheritance is a feature of Object Oriented Programming which allows an Object to use the properties and behavior of parent class. Inheritance supports re-usability of code. So as discussed in above example, all models of Car acquire the properties of parent “Car”.

Encapsulation

Before OOPs concept was introduced, data was unsecured and travelling without any access restriction. Anyone was able to access and modify the data like in case of “C” programming language. Today, data is an important asset for any individual, group or company. So you need to protect your information from unauthorized  access and modification. And you can simply do this with “Encapsulation”.

encapsulation in java
encapsulation in java


Encapsulation is used to implement abstraction in Object Oriented Programming. Encapsulation allows you to protect your information and add some access rights. Now you will be able to decide what should be accessible to the public and what should be private.

Abstraction

To understand Abstraction, lets take an example first.

You drive car, ride bike, use mobile phones, watch TV etc in your day to day life. But did you every think, how these things working internally? How your mobile phone is able to connect with the person who is miles away from you? How you are able to watch hundreds of channels on your TV? What’s going on internally? The truth is, we don’t care. We don’t care how they are working and which component is responsible to perform what operation. We just want to drive our car, watch channels on TV, call from our cell phone. What’s going inside is none of our business.

And that is Abstraction. Abstraction is the feature of Object Oriented Programming which hides complexity and represents things in simple and easy manner.  Internal processing is hidden from the outside world and only the basic functionality is shown.

Polymorphism

Let’s divide Polymorphim to understand better.

Poly means many, more than one.

Morphmeans different forms, change.

So, Polymorphism is the feature of Object Oriented Programming where an Object has same interface for different underlying forms.

Let’s take an example of an website. We go through different websites in our day to day life. Like facebook, gmail, you tube etc. Each website has different buttons like add comment, like, login, logout etc. But it is simply a “Button”, and you have to just press the button. So, button is an interface for different underlying forms. Button can be used to add a comment, can be used to like picture etc.

So, a Button on a website can be a simple example of Polymorphism.

Leave a Reply

Your email address will not be published. Required fields are marked *