Object-Oriented Programming in Java — Java OOPs Concepts

Dammika Rajapaksha
4 min readMay 12, 2021

This article will give you an understanding of the OOPs concept in Java. Object-Oriented Programming (OOP) refers to a type of programming in which programmers define the data type of a data structure and the type of operations that can be applied to the data structure.

Object

An entity that has state and behavior is known as an object e.g., Table, car, fan, pencil, bed, etc. It can be physical or logical (tangible and intangible).

The object has three characteristics

  1. State
  2. Behavior
  3. Identity

Class

Class is a template or blueprint of the object. It is a logical entity. It can’t be physical.

class in Java can contain

  • Fields
  • Methods
  • Constructor
  • Blocks
  • Nested class and interface

Interface

An interface in Java is a blueprint of a class. It has static constants and abstract methods. The interface in java is a mechanism to achieve abstraction. There can be only abstract methods in the Java interface, not the method body. It is used to achieve abstraction and multiple inheritances in Java.

example for interface

Polymorphism

Polymorphism in Java is a concept by which we can perform a single action in different ways.

There are two types of polymorphism in Java:

1.compile-time polymorphism

2.runtime polymorphism

We can perform polymorphism in java by method overloading and method overriding.

example for method overloading

There are two ways to overload the method in java

  1. By changing number of arguments
  2. By changing the data type
By changing number of arguments
By changing the data type

example for method overriding

Abstraction

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

Rules for abstraction class

Example of Abstract class that has an abstract method

Advantages of Abstraction

  1. It reduces the complexity of viewing the things.
  2. Avoids code duplication and increases reusability.
  3. Helps to increase security of an application or program as only important details are provided to the user.

Encapsulation

Encapsulation in Java is a process of wrapping code and data together into a single unit

Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.

To achieve encapsulation in Java −

  • Declare the variables of a class as private.
  • Provide public setter and getter methods to modify and view the variables values.

Advantages of encapsulation

  1. The encapsulated code is more flexible and easy to change with new requirements.
  2. It prevents the other classes to access the private fields.
  3. Encapsulation allows modifying implemented code without breaking other code who have implemented the code.
  4. It keeps the data and codes safe from external inheritance. Thus, Encapsulation helps to achieve security.
  5. It improves the maintainability of the application.

Example of Encapsulation in Java

I hope you got a clear understanding about OOP concepts from this article. Practice more and more of it by applying it to your work

--

--