Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Tutorial
  • Java Spring
  • Spring Interview Questions
  • Java SpringBoot
  • Spring Boot Interview Questions
  • Spring MVC
  • Spring MVC Interview Questions
  • Java Hibernate
  • Hibernate Interview Questions
  • Advance Java Projects
  • Java Interview Questions
Open In App
Next Article:
POJO vs Java Beans
Next article icon

POJO vs Java Beans

Last Updated : 01 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

POJO stands for Plain Old Java Object. It is an ordinary Java object, not bound by any special restriction other than those forced by the Java Language Specification and not requiring any classpath. POJOs are used for increasing the readability and re-usability of a program. POJOs have gained the most acceptance because they are easy to write and understand. They were introduced in EJB 3.0 by Sun Microsystems.

Properties of POJO

  1. Extend prespecified classes, Ex: public class GFG extends javax.servlet.http.HttpServlet { ... } is not a POJO class.
  2. Implement prespecified interfaces, Ex: public class Bar implements javax.ejb.EntityBean { ... } is not a POJO class.
  3. Contain prespecified annotations, Ex: @javax.persistence.Entity public class Baz { ... } is not a POJO class.

POJOs basically define an entity. Like in your program, if you want an Employee class, then you can create a POJO as follows:

Java
// Employee POJO class to represent entity Employee public class Employee {     // default field     String name;      // public field     public String id;      // private salary     private double salary;      //arg-constructor to initialize fields     public Employee(String name, String id,                               double salary)     {         this.name = name;         this.id = id;         this.salary = salary;     }      // getter method for name     public String getName()     {         return name;     }      // getter method for id     public String getId()     {         return id;     }      // getter method for salary     public Double getSalary()     {         return salary;     } } 

Explaination of the above program:

The above example is a well-defined example of the POJO class. As you can see, there is no restriction on access-modifiers of fields. They can be private, default, protected, or public. It is also not necessary to include any constructor in it.
POJO is an object which encapsulates Business Logic. The following image shows a working example of the POJO class. Controllers interact with your business logic which in turn interact with POJO to access the database. In this example, a database entity is represented by POJO. This POJO has the same members as the database entity.

Java_bean


Java Beans

Beans are special type of Pojos. There are some restrictions on POJO to be a bean.

  1. All JavaBeans are POJOs but not all POJOs are JavaBeans.
  2. Serializable i.e. they should implement Serializable interface. Still, some POJOs who don't implement a Serializable interface are called POJOs because Serializable is a marker interface and therefore not of many burdens.
  3. Fields should be private. This is to provide complete control on fields.
  4. Fields should have getters or setters or both.
  5. A no-arg constructor should be there in a bean.
  6. Fields are accessed only by constructor or getter setters.

Getters and Setters have some special names depending on field name. For example, if field name is someProperty then its getter preferably will be:

public "returnType" getSomeProperty()
{
return someProperty;
}

and setter will be

public void setSomePRoperty(someProperty)
{
this.someProperty=someProperty;
}

Visibility of getters and setters is generally public. Getters and setters provide the complete restriction on fields. e.g. consider below the property,

Integer age;

If you set visibility of age to the public, then any object can use this. Suppose you want that age can't be 0. In that case, you can't have control. Any object can set it 0. But by using the setter method, you have control. You can have a condition in your setter method. Similarly, for the getter method if you want that if your age is 0 then it should return null, you can achieve this by using the getter method.

Below is the implementation of the above topic:

Java
// Java program to illustrate JavaBeans class Bean implements Serializable {     // private field property     private Integer property;     public Bean()     {         // No-arg constructor     }      // setter method for property     public void setProperty(Integer property)     {         if (property == 0) {             // if property is 0 return             return;         }         this.property = property;     }      // getter method for property     public Integer getProperty()     {         if (property == 0) {             // if property is 0 return null             return null;         }         return property;     } }  // Class to test above bean public class GFG {     public static void main(String[] args)     {         Bean bean = new Bean();          bean.setProperty(0);         System.out.println("After setting to 0: "                            + bean.getProperty());          bean.setProperty(5);         System.out.println("After setting to valid"                            + " value: "                            + bean.getProperty());     } } 

Output:

After setting to 0: null
After setting to valid value: 5

POJO vs Java Bean

POJO

Java Bean

It doesn't have special restrictions other than those forced by Java language.It is a special POJO which have some restrictions.
It doesn't provide much control on members.It provides complete control on members.
It can implement Serializable interface.It should implement serializable interface.
Fields can be accessed by their names.Fields are accessed only by getters and setters.
Fields can have any visibility.Fields have only private visibility.
There may/may-not be a no-arg constructor.It 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 entityIt is used when you want to provide user your entity but only some part of your entity.

Conclusion

POJO classes and Beans both are used to define java objects to increase their readability and reusability. POJOs don't have other restrictions while beans are special POJOs with some restrictions.
 


Next Article
POJO vs Java Beans

V

Vishal Garg
Improve
Article Tags :
  • Advance Java

Similar Reads

    Spring vs Struts in Java
    Understanding the difference between Spring and Struts framework is important for Java developers, as both frameworks serve distinct purposes in building web applications. The main difference lies in their design and functionalitySpring: Spring is a comprehensive, modular framework offering dependen
    3 min read
    Spring - BeanPostProcessor
    Spring Framework provides BeanPostProcessor Interface. It allows custom modification of new bean instances that are created by the Spring Bean Factory. If we want to implement some custom logic such as checking for marker interfaces or wrapping beans with proxies after the Spring container finishes
    5 min read
    What is Spring Data JPA?
    Spring Data JPA is a powerful framework that simplifies database access in Spring Boot applications by providing an abstraction layer over the Java Persistence API (JPA). It enables seamless integration with relational databases using Object-Relational Mapping (ORM), eliminating the need for boilerp
    6 min read
    Java Spring - Using @Scope Annotation to Set a POJO's Scope
    In the Spring Framework, when we declare a POJO (Plain Old Java Object) instance, what we are essentially creating is a template for a bean definition. This means that, just like a class, we can have multiple object instances created from a single template. These beans are managed by the Spring IoC
    7 min read
    Spring Boot - Dependency Injection and Spring Beans
    Spring Boot is a powerful framework for building RESTful APIs and microservices with minimal configuration. Two fundamental concepts within Spring Boot are Dependency Injection (DI) and Spring Beans. Dependency Injection is a design pattern used to implement Inversion of Control (IoC), allowing the
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences