Java – Object-Oriented Programming concept3 min read

Object-oriented programming:

It is the concept of using objects in programming. It is a paradigm that uses objects and classes that aim to implement real-world entities. The entities such as inheritance, abstraction, polymorphism, etc that are used in programming.

The main goal of an OOP is to tie together the data and its method in a single object so that no other block of code can access that data but only that function. And thus, it also makes it easier to work with.

The main principle of OOP are:

  • Inheritance. 
  • Encapsulation. 
  • Abstraction. 
  • Polymorphism

Inheritance:

It is one of the main features of Object-Oriented Programming (OOP). Inheritance is one of the processes or mechanisms in OOP in which one class(sub-class) acquires the properties(data members) and functionalities(methods) of another class(parent-class).

Super Class: The class whose properties and functionalities are inherited by the Subclass is known as the superclass(a parent class or a base class).

Sub Class: The class that inherits the property and behavior of the other class(Superclass) is known as subclass( a derived class, extended class, or child class).

Syntax:

Also Learn Types of Inheritance with Diagram and Example:


Encapsulation:

Encapsulation is one of the four fundamental OOP concepts. Encapsulation in Java is defined as the wrapping up of data(variable) under a single unit. The use of Encapsulation is to make sure that implementation detail or we can say sensitive data is hidden from the users.

For this, Encapsulation is also known as data hiding.

Benefits of Encapsulation:

  • Data- hiding in Java.
  • The class field can be made read-only or write-only.
  • It provides the class the total control over the data.

Syntax Example:


Abstraction:

Data abstraction is the process of hiding the details but only displaying the relevant information to the users, which is hiding the implementation details and displaying only its functionalities. This is one of the main advantages of using abstraction.

Abstraction is one of the four major concepts behind object-oriented programming (OOP).

In java, abstraction can be achieved in two ways.

  • Abstraction class.
  • Interfaces.

1. Abstraction class.

  • An abstract class is a class that is declared with an abstract keyword.
  • An abstract class may or may not have abstract methods.
  • This class cannot create objects, to access this class, it must be inherited.

2. Interfaces

An Interface in Java is the same as class, like any other class, an interface can have methods and Variables.


Polymorphism:

Polymorphism means having many forms. The word “poly” means many and “morphs” means forms. In java, it is a concept that allows the user to perform a single action in different ways.

In Java polymorphism is mainly divided into two types:

1. Compile-time Polymorphism: 

It is also known as static polymorphism. In this type of program, the flow of control is decided at compile time itself and if any error found, they are resolved at compile time. This can be achieved by method overloading.

2. Runtime Polymorphism:

It is also known as Dynamic Method Dispatch. It is a process in which a call to an overridden method is resolved at runtime. This type of polymorphism is achieved by Method Overriding.


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …