POJO Class

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

POJO class: POJO class means a class with a setter and getter method using a setter method

We can store data in object and using a getter method we can retrieve data from object.
The standard of Pojo class Design is declare variable as private and create set and get method or
function with a variable name.
POJO class work as Container class

package org.techhub;

class Employee {
private int id;
private String name;

public int getId() {


return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class PojoClassApplication {
public static void main(String[] args) {
Employee emp = new Employee();
emp.setId(1);
emp.setName("Ram");
System.out.println(emp.getId()+"\t"+emp.getName());
}
}

How to pass object as parameter in another class function


___________________________________________________________________

Q. why we need to pass object as parameter in another class function?


______________________________________________________________________
some time if we have function with a different type of data and if we have large number
of parameter of different type in function then passing single single parameter is not
good approach so better way we can pass object as parameter.
Example: Suppose consider we have class name as Admission in Admission class two methods

void newAdmission(String name,int id,float fees,String address,String branche,Date dob):


this method can accept student details for new admission
void show(): this method can display the student data .

package org.techhub;
class Admission{
private String name;
private int id;
private float fees;
private String address;
void newAdmission(String name,int id,float fees,String address) {
this.name=name;
this.id=id;
this.fees=fees;
this.address=address;
}
void show() {
System.out.println(name+"\t"+id+"\t"+fees+"\t"+address);
}
}
public class AdmissionApplication {
public static void main(String[] args) {
// TODO Auto-generated method stub
Admission ad = new Admission();
ad.newAdmission("Ram", 1, 90.0f, "PUNE");
ad.show();
}
}

if we think about above code we have function name as newAdmission() and in this function
contain four types of data of different type but when we large number of parameter list of
different type then it is very complicated to remember every parameter type ,every type sequence
and number of parameter list so better we can store all data in single object and pass object
as parameter where we want to send multiple type of data.
Source code
_______________________________________________________________________
package org.techhub;

class Student {
private String name;

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public int getId() {


return id;
}

public void setId(int id) {


this.id = id;
}

public float getFees() {


return fees;
}

public void setFees(float fees) {


this.fees = fees;
}

public String getAddress() {


return address;
}

public void setAddress(String address) {


this.address = address;
}

private int id;


private float fees;
private String address;

class Admission {
Student s;
void newAdmission(Student s) {
this.s=s;
}
void show() {
System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getFees()+"\t"+s.getAddress());
}
}

public class AdmissionApplication {


public static void main(String[] args) {
Admission ad = new Admission();
Student s1 = new Student();
s1.setId(1);
s1.setName("ABC");
s1.setAddress("PUNE");
s1.setFees(100000);
ad.newAdmission(s1);
ad.show();

}
}
Object with variable arguments or reference with variable arguments.

Example:
We want to create application small shop for generate bill
and we have three classes in this application
1) Product: Product is POJO means it contain only setter and getter and Product contain
Following information or data or variable id, name, price, qty,company name
Use setter and getter method with all variables.

2) Bill : Bill class contain method


void calBill(String custName,String custContact,Product …p):
this method contain three parameters first is cust name ,second is custcontact
and third is infinite product references or objects means single customer can
purchase infinite number of products.

3) ShopApplication: this class contain main method where we crete 5 Product and
store information in Project object using a setter method and create object of Bill class
and call its calBill() method and pass customer name, customer contact and 5 product
objects in it and generate following type of output

You might also like