There are two types of methods in java with respect to the
manipulations over object state.
1.
Mutator methods
2.
Accessor methods
Q) What is the difference between
mutator methods and accessor methods?
Ans):-
Mutator method is a java method, it can be used to
set/modify data inside object.
Ex:-
All setXXX(--)methods in java bean classes are treated as
"mutator methods"
Accessor method is a java method, it can be used to get data
from an object.
Ex:-
All getXXX(--)methods in java bean classes are treated as
"Accessor methods"
Note:-
Java bean is a class containers variables and the respective
setXXX() method and getXXX() methods.
Ex diagram:-
Date july-20-2016
Ex code:-
class Employee
{
private
String eid;
private
String ename;
private
float esal;
private
String eaddr;
public
void setEid(String eid1) //Mutator Method
{
eid=eid1;
}
public
void setEname(String ename1) //Mutator Method
{
ename=ename1;
}
public
void setEsal(float esal1) //Mutator Method
{
esal=esal1;
}
public
void setEaddr(String eaddr1) //Mutator Method
{
eaddr=eaddr1;
}
public
String getEid() //Accessor Method
{
return
eid;
}
public
String getEname() //Accessor Method
{
return
ename;
}
public
float getEsal() //Accessor Method
{
return
esal;
}
public
String getEaddr() //Accessor Method
{
return
eaddr;
}
}
class Test
{
public
static void main(String[] args)
{
Employee
emp=new Employee();
emp.setEid("E-111");
emp.setEname("Sai");
emp.setEsal(50000);
emp.setEaddr("Hyderabad");
System.out.println("Employee
Details");
System.out.println("-----------------");
System.out.println("Employee
Id :"+emp.getEid());
System.out.println("Employee
Name :"+emp.getEname());
System.out.println("Employee
salary :"+emp.getEsal());
System.out.println("Employee
Address :"+emp.getEaddr());
}
}
Output:-
Employee details
___________________
Employee Id
:E-111
Employee Name
:Sai
Employee Salary
:50000.0
Employee Address
:Hyderabad

No comments:
Post a Comment