Table of contents
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:
This class should not implement any interface.
This class should not extend to any class.
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:
Its variables should be declared private.
Variables getter and setter methods should be public.
The class should have a zero arg constructor (basically by default a Java class has a zero args constructor).
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.