POJO in java

Previous
Next

Encapsulation:

  • Encapsulation is the concept of protecting object functionality from outside access.
  • Writing data (variables) and code (methods) into a single unit.
  • In the communication world, we should make outer visibility of Object but not properties and methods.

Note: Object information is sensitive and that has to be protected. Encapsulation is the concept of protecting object information. Class is used to implement encapsulation technically. POJO is the best example of how to protect information of a class.

POJO: Stands for ‘Plain Old Java Object’. Every object is protected if it is defined as POJO. In java app, object should follow POJO rules.

  1. Class must be public: Hence object is visible to all other object in communication process.
  2. Properties must be private: Information is not visible to other object – hence objects cannot access the data directly.
  3. Every property has getter and setter: Communication is possible using methods. Two standard methods get() and set() are given to send and receive the information.
  4. Every class must have default constructor: Using default constructor, we can provide default values as object initialization. For example, balance in account is 0 as soon as account object has created.
  5. Can have optional arguments constructor:  We can also set initial values instead of default values if required. For example, in some of the banks we can open account with minimum deposit amount.

Getter method & Setter Method:

  • Object data cannot be accessed directly by another object.
  • To get the information of an object, communication is required.
  • Objects communication is possible using methods (setter and getter).
  • Getter method returns info and doesn’t take input
  • Setter method takes input but doesn’t return output

For example:

private int balance ;
public int getBalance()
{
	return this.balance;	
}
public void setBalance(int balance)
{
	this.balance = balance ;
}

Simple POJO class:

public class POJO 
{
	private int a, b;
	public POJO()
	{
		// Initialize object with default values....
	}
	public POJO(int a, int b)
	{
		this.a = a;
		this.b = b;
	}
	public int getA()
	{
		return this.a;
	}
	public int getB()
	{
		return this.b;
	}
	public void setA(int a)
	{
		this.a = a;
	}
	public void setB(int b)
	{
		this.b = b;
	}
}

Accessing POJO class:

class Access 
{
	public static void main(String[] args) 
	{
		POJO obj = new POJO();
		System.out.println("a val : " + obj.getA());
		System.out.println("b val : " + obj.getB());
	}
}

Another way of accessing POJO class:

class Access 
{
	public static void main(String[] args) 
	{
		POJO obj = new POJO();
		System.out.println("a val : " + obj.getA());
		System.out.println("b val : " + obj.getB());

		POJO obj1 = new POJO();
		obj1.setA(10);
		obj1.setB(20);
		System.out.println("a val : " + obj1.getA());
		System.out.println("b val : " + obj1.getB());

		POJO obj2 = new POJO(30,40);
		System.out.println("a val : " + obj2.getA());
		System.out.println("b val : " + obj2.getB());
	}
}

EMP POJO class:

public class Employee 
{
	private int num;
	private String name;
	private double salary;
	public Employee()
	{
		// provide default values....
	}
	public Employee(int num, String name, double salary)
	{
		this.num = num ;
		this.name = name ;
		this.salary = salary ;
	}
	public int getNum()
	{
		return this.num;
	}
	public void setNum(int num)
	{
		this.num = num;
	}
	public String getName()
	{
		return this.name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public double getSalary()
	{
		return this.salary;
	}
	public void setSalary(double salary)
	{
		this.salary = salary;
	}
}

Access Employee POJO:

class AccessEmployeePOJO 
{
	public static void main(String[] args) 
	{
		Employee e1 = new Employee();
		// e1.eno = 101 ; /*Error : Data cannot be accessed directly */
		e1.setEno(101);
		e1.setEname("Amar"); /* Access data using functionality */

		Employee e2 = new Employee(102 , "Annie");

		System.out.println("e1 - ename : "+e1.getEname());
		System.out.println("e2 - ename : "+e2.getEname());
	}
}
Previous
Next

Add Comment

Courses Enquiry Form