Pojo  Vs  Java Bean In Java

Pojo Vs Java Bean In Java

So, you have heard these two words if you are in the Java development field, but many of them still don't clearly understand these two concepts or they have a misconception that both are the same.

In this article, I will give you a clear and concise understanding of these two terms in Java.

First of all, both are classes in Java i.e. POJO Class and Java Bean Class. Now let me tell you the basic difference between these two in detail.

POJO Class (plain old Java object)

For any class to be called a POJO class, there are three factors to be considered:

  1. This class should not implement any interface.

  2. This class should not extend to any class.

  3. This class does not have any annotations.

And the object of this class will be called Plain Old Java Object.

Let me show an example, of what a POJO Class looks like :

Explaination: The CricketPlayer class is a POJO class because :

it does not extend any class, it implements any interface nor has any annotations present in the class.

Let me show an example, of what is not a POJO Class :

Explanation: Above class i.e., CricketPlayer1 is not a POJO class as it extends Class Player.

JAVA BEANS CLASS

For a Java Bean Class, One Class should possess four features:

  1. Its variables should be declared private.

  2. Variables getter and setter methods should be public.

  3. The class should have a zero arg constructor (basically by default a Java class has a zero args constructor).

  4. The class must implement a java.io.serializable interface.

    Let me give an example to have a better understanding of what a Java Bean Class is:

    Explanation: The above class is Java Bean as it implements java.io.serializable,it's variables are declared private, it has a zero arg constructor(u can skip it also, as it is by default present in Java Class) and all the getters and setters methods are public.

Did you find this article valuable?

Support Gaurav Sharma by becoming a sponsor. Any amount is appreciated!