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.

package PojoPayloads;

public class Employee {
	
	// fields to store data
	public String firstName;
	public String lastName;
	public double salary;
	public String cityName;
	public boolean isMarried;
	public char gender;
	
	// Create employees with different data
	public Employee(String firstName, String lastName, double salary, String cityName, boolean isMarried,
			char gender) {
		super();
		this.firstName = firstName;
		this.lastName = lastName;
		this.salary = salary;
		this.cityName = cityName;
		this.isMarried = isMarried;
		this.gender = gender;
	}
	
	// Public getter setter methods
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	public String getCityName() {
		return cityName;
	}
	public void setCityName(String cityName) {
		this.cityName = cityName;
	}
	public boolean isMarried() {
		return isMarried;
	}
	public void setMarried(boolean isMarried) {
		this.isMarried = isMarried;
	}
	public char getGender() {
		return gender;
	}
	public void setGender(char gender) {
		this.gender = gender;
	}
	
}

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

package PojoPayloads;

public class EmployeeDetails {

	public static void main(String[] args) {
		
		Employee amod = new Employee("Amod", "Mahajan", 100000, "Benagluru", false, 'M');
		Employee animesh = new Employee("Animesh", "Prashant", 200000, "Benagluru", true, 'M');
		
		// Get married status of Amod
		System.out.println("Is Amod married? : "+amod.isMarried());
		
		// Amod is married now and change data
		amod.setMarried(true);
		System.out.println("Is Amod married now ? : "+amod.isMarried());
		
		// Increase salary of animesh
		animesh.setSalary(500000);
		System.out.println("Updated salary of animesh : "+ animesh.getSalary());
	}
}
 

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

package PojoPayloads;

public class EmployeePojo {
	
	// Private fields
	private String firstName;
	private String lastName;
	private double salary;
	private String cityName;
	private boolean isMarried;
	private char gender;
	private String fullName;
	
	// Public constructor
	public EmployeePojo()
	{
		
	}
	
	// Business logic to get full name
	public String getFulName()
	{
		this.fullName =  this.firstName + " "+ this.lastName;
		return fullName;
	}
	
	// Public getter setter methods
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	public String getCityName() {
		return cityName;
	}
	public void setCityName(String cityName) {
		this.cityName = cityName;
	}
	public boolean isMarried() {
		return isMarried;
	}
	public void setMarried(boolean isMarried) {
		this.isMarried = isMarried;
	}
	public char getGender() {
		return gender;
	}
	public void setGender(char gender) {
		this.gender = gender;
	}
	
}

Usage of Employee class

package PojoPayloads;

public class EmployeePojoUsage {
	
	public static void main(String[] args) {
		
		EmployeePojo Amod;
		EmployeePojo Animesh;
		
		// Setting employees details 
		Amod = new EmployeePojo();
		Amod.setFirstName("Amod");
		Amod.setLastName("Mahajan");
		Amod.setCityName("Benagluru");
		Amod.setGender('M');
		Amod.setMarried(false);
		Amod.setSalary(10000.54);
		
		Animesh = new EmployeePojo();
		Animesh.setFirstName("Animesh");
		Animesh.setLastName("Prashant");
		Animesh.setCityName("Kolkata");
		Animesh.setGender('M');
		Animesh.setMarried(false);
		Animesh.setSalary(23232.45);
		
		// Printing details of employees
		System.out.println("Details of Employees :-");
		System.out.println("First Name : "+ Amod.getFirstName());
		System.out.println("Last Name  : "+ Amod.getLastName());
		System.out.println("Full Name  : "+ Amod.getFulName());
		System.out.println("City Name  : "+ Amod.getCityName());
		System.out.println("Is Married?: "+ Amod.isMarried());
		System.out.println("Gender     : "+ Amod.getGender());
		System.out.println("Salary     : "+ Amod.getSalary());
		
		
		System.out.println("==========================================");
		System.out.println("First Name : "+ Animesh.getFirstName());
		System.out.println("Last Name  : "+ Animesh.getLastName());
		System.out.println("Full Name  : "+ Animesh.getFulName());
		System.out.println("City Name  : "+ Animesh.getCityName());
		System.out.println("Is Married?: "+ Animesh.isMarried());
		System.out.println("Gender     : "+ Animesh.getGender());
		System.out.println("Salary     : "+ Animesh.getSalary());
		
	}
}

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.
#ThanksForReading
#HappyLearning

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.

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

Your email address will not be published. Required fields are marked *