MainMenu

Home Java Overview Maven Tutorials

Saturday, 16 April 2022

POJO Class in Java

POJO Class in Java with example



Hello Friends,

This article will describe the concept of pojo class in java.



POJO Class in Java

:-


POJO stands for :-
PLAIN OLD JAVA OBJECT

Pojo is an ordinary object and pojo is not bound by any special restriction.
Also pojo file does not require any special classpath.
It increases the readability and re-usability of a java program.
History :-
In 2000, The term POJO was introduced by Martin Fowler ( An American software developer). it is available in Java from the EJB 3.0 by sun microsystem.

Rule :-
1) Class must be public
2) The object in the POJO Class can have any access modifies such as private, public, protected. But, all instance variables should be private for improved security of the project
3) Must have public default constructor
4) can have args constructor
5) Every property/field/variable must have publc getter and setter method
6) A POJO class should not extend predefined classes.
7) It should not implement prespecified interfaces.
8) It should not have any prespecified annotation.

Pros of Pojo classes :-
1) It increases the readability and re-usability of a java program.
2) POjo is easy to write and Understand



Example of POJO Class
public class Pojoexample
{
private String name;
private int age;
public String getName()
{
return name;
}
public void setName(String Name)
{
this.name = Name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
}


-------------------------------------------------------------------
public class pojoextend extends Pojoexample
{
}
-------------------------------------------------------------------
public class PojorunExample
{ public static void main(String args[])
{ // Pojoexample pojoobject = new Pojoexample();
pojoextend pojoobject = new pojoextend();
pojoobject.setName("Chandan Singh");
pojoobject.setAge(30);
System.out.println(pojoobject.getName());
System.out.println(pojoobject.getAge());
}
}
Output :- Chandan Singh
30

-------------------------------------------------------------------
POJO Class Example by using Arguments Constructor
public class Pojoexample
{
private String name;
private int age;
Pojoexample(String name, int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}



POJO Vs JAVA Beans


POJO Java Beans
Pojo does not have special restrictions other than those forced by java languageJava Beans is a special POJO which have some restrictions.
It doesn't provide much control on membersIt provide complete control on members
Pojo can implement the serializable interface, but it is not mandatory The Bean class should implement the Serializable interface
Pojo class can be accessed by using their namesThe Bean class can only be accessed by using the getters and setters.
Fields may have any of the access modifiers such as public, private, protechted. Fields can only have private access.
In POJO, it may or may not have no-ar constructorIt must have a no-arg constructor
It is used when you don’t want to give restriction on your members and give user complete access of your entity It is used when you want to provide user your entity but only some part of your entity.


Interview Question

Q1). Can we extend POJO class?
Answer :- Yes, one class can extend pojo class.

Q2). What are three rule of POJO class?
Answer :- Rules are :- .
1) A POJO class should not extend predefined classes.
2) It should not implement prespecified interfaces.
3) It should not have any prespecified annotation.