Object Oriented Programming (OOPs) Concept in Java.

Introduction

Object-Oriented Programming (OOPs) is the concept of objects based programming, that contains data(fields) and its behavior (method), in a single location(object).

So the reason for using object-oriented programming is to increase the flexibility and maintainability of programs.

That’s why in object-oriented programming language everything is represented as an object.

The main aim of Object-oriented programming to implement real-world entities like inheritance, hiding(Abstraction and Encapsulation), polymorphism, etc. in programming.

 

OOPs Concept

 

List of OOP Concept in Java.

  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

 

There are four main OOP concept in Java.

  • Polymorphism
  • Inheritance
  • Abstraction
  • Encapsulation

 

Let’s get started with the first Object and Class OOPs concept.

What is the Class?

We can simply say a Class is a user-defined data type, which has a group of objects and common properties.
or we can say it is a blueprint or template from which objects are created. It is a logical entity.

 

What is an Object?

We can simply say, an object is an instance of a class. It is a real-world entity like a pen, dog, table, etc.  An object in Java is a physical as well as a logical entity.

For more details of the Object and method click here.

 

Now let’s get discuss four main OOP concepts.

What is Polymorphism?

Polymorphism is one of the OOPs features, it allows us to perform a single action in different ways.

Polymorphism word comes from 2 Greek words. Poly and morphs, Poly means many and morphs means forms. That’s why can say, polymorphism means many forms.

 

Simple polymorphism example.

public class Employee{
public void test(){
System.out.println("Animal is making a sound"); 
}
}

public class Office extends Employee{
@Override
public void going(){
System.out.println("Going to Office");
}
}

public class Market extends Employee {
@Override
public void going() {
System.out.println("Gogin to Market");
}
}

In the above example with the same method name, we are performing different activities.  As you can see that although we had the same action for all subclasses going(), there were different ways to do the same action.

 

Types of polymorphism in Java.

There are two types of polymorphism in Java.

  • Compile-time polymorphism
  • Runtime polymorphism

For details of polymorphism and its type click here.

 

What is Inheritance?

Inheritance is a mechanism, where one class acquires all the properties and behaviors (methods and fields) of another class is called Inheritance.

We use extends Keyword to inherit the properties of one class to another class.

Using the extends keyword, the child class will be able to inherit all the properties of the parent class but except for the private properties of the parent class.

 

The syntax of Java Inheritance Example:

class Parent {
.....
.....
}

class Child extends Parent {
.....
.....
}

 

In the above example Parent class is called a superclass, child class is called a subclass, where child class extends the Parent class and gets all properties and behaviors of the parent class.

 

Why use inheritance in java?

  • For Method Overriding
  • For Code Re-usability

For more details of Inheritance click here.

 

What is Abstraction?

Abstraction is a process of hiding the implementation details, and showing only functionality to the user.

The main purpose of abstraction is to hide the unnecessary details from the users.

In another way, we can say abstraction is shown only essential things to the user and hides the internal details from the user.

Let’s try to understand with an example, we are withdrawing money from ATM, so we are entering the ATM card and PIN and amount and you get the money, but we don’t know what is internally going on in this process.

So in this process. We are only able to see essential things like entering the password, enter the amount. But we are not able to see the internal process of that transaction.

 

How to achieve Abstraction in java?

There are two ways to achieve abstraction in java.

  • Abstract class (0 to 100%)
  • Interface (100%)

For more details of abstraction click here.

 

What is Encapsulation?

It is wrapping code(method) and data (variables) together into one single unit.

A real-life example of Encapsulation is a Capsule. In that capsule, several medicines are mixed, and it is wrapped into one outer layer.

 

How to create a fully Encapsulation class in java?

By making all the data members of the class private. We are creating public setter and getter methods, in the class to set and get the data.

The Java Entity class (Bean class), is fully encapsulated in java.

 

Advantage of Encapsulation in Java.

There are many advantages of Encapsulation in java.

  • Make read-only and write-only classes using encapsulation.
  • Achieve data hiding.
  • The encapsulated classes are better for unit testing.
  • Easy and fast.
  • Control over data.

For more details of Encapsulation click here.

 

Read more topics related to java

 

Hope this was helpful for you. If you have any questions, please feel free to leave a comment. Thank you for reading.

 

 

Leave a Reply

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