Make Selenium Easy

REST Assured Tutorial 28 – What is Plain Old Java Object (POJO) ?

As a part of End to End REST Assured Tutorial , in this post We will learn about POJO.

Java supports Object Oriented Programming System (OOPs) which is a programming paradigm. From the full form of OOPs , we can understand that it is a programming approach which is oriented around something called “Object” and an Object can contain data (Member variables ) and mechanism (member functions) to manipulate on these data.

For an example – A company has many employees and there are some basic details which need to be stored in such a way that it can be accessed and manipulated easily. If we create a blue print of an Employee which defines what are data to be stored and mechanisms to manipulate on those data then our problem can be solved.

We can create a Class “Employee” ( We can give any meaningful name) and to store employee related data we can declare fields or variables in this class. We also need to retrieve and manipulate data so we need to define some mechanism or methods to do so.

Now you can store all employees details in separate objects using same blueprint or class.

I have written very simple business logic to work on data above. You can do however you want to do. For example :- Hike salary by percentage rather than setting a value.

In above program , “amod” and “animesh” are ordinary Java objects or POJO.

POJO stands for Plain Old Java Object and it is an ordinary Java objects not a special kind of. The term POJO was coined by Martin Fowler, Rebecca Parsons and Josh MacKenzie in September 2000 when they were talking on many benefits of encoding business logic into regular java objects rather than using Entity Beans or JavaBeans.

“We wondered why people were so against using regular objects in their systems and concluded that it was because simple objects lacked a fancy name. So we gave them one, and it’s caught on very nicely. ” – Martin Fowler

As per Wikipedia :-

The term “POJO” initially denoted a Java object which does not follow any of the major Java object models, conventions, or frameworks.

How To Make POJO More Effectively

A POJO should be created in a way so that it can be utilized effectively and in an easy manner. In above example all fields were declared public. It means any one can access those variables directly and manipulate. A POJO created with some guidelines helps in designing better architecture for data storing and manipulating.

A POJO class can follow some rules for better usability. These rules are :-

  1. Each variable should be declared as private just to restrict direct access.
  2. Each variable which needs to be accessed outside class may have a getter or a setter or both methods. If value of a field is stored after some calculations then we must not have any setter method for that.
  3. It Should have a default public constructor.
  4. Can override toString(), hashcode and equals() methods.
  5. Can contain business logic as required.

There are some restrictions imposed by Java language specifications on POJO. A POJO should not :-

  1. Extend prespecified classes
  2. Implement prespecified interfaces
  3. Contain prespecified annotations

Following above guidelines we can restructure above POJO as below :-

Employee Class

Usage of Employee class

Advantages of POJO :-

  1. Increases readability
  2. Provides type checks
  3. Can be serialized and deserialized
  4. Can be used anywhere with any framework
  5. Data Manipulation is easier. We can have logic before setting and getting a value.
  6. Builder pattern can be used with POJO.
  7. Frequently used to create payloads for API.
  8. Reusability

You can download/clone above sample project from here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

Find all Selenium related post here, all API manual and automation related posts here and find frequently asked Java Programs here.

Many other topics you can navigate through menu.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

3 thoughts on “REST Assured Tutorial 28 – What is Plain Old Java Object (POJO) ?

  1. To make a POJO effective, you recommended “If value of a field is stored after some calculations then we must not have any setter method for that.” Can you please tell more about it, maybe with an example?

    1. Consider you want to set age of a person by passing year of birth, then some calculation is needed, so you should not have a setter method like setAge(1992) , because you will not store 1992 as it is.

      you will have to do calculation to find age, so have a calculation in a function called setAge() instead of using setter method

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.