An implementation of Getters and Setters Methods in Java
Getter and setter emanate from the concept of Encapsulation which a fundamental concept of OOP as discussed in a recent blog post. In ecapsulation, much of the mechanisms is matters data hiding which entails the variables of a class being hidden from other classes, and this particular class being accessed only through the methods of their current class. In a nutshell, getter-setter methods simply mean:
Look but don't touch
Explaining getter-setter concepts to a beginner maybe quite complex especially if they have no initial exposure to OOP o even programming at all. Let's take the example of people having fun in a party. Let's assume these people to be objects. They have personal information which we will call"attributes". Some of this personal information is sensitive and they do not want every body to know. Say for example, someone does not want to reveal their mobile number to the public. Those are pieces of information these objects (or party people) will declare as "private" There is some other information they do want to reveal to the public. For example, they may want to reveal their name to their new acquaintances. This is the kind of attributes or pieces of information they could declare as "public"
Revealing personal attributes as public allows others not only to read the information but also to modify it. And this is the reason our objects would rather protect certain information by declaring it private (So other objects cannot access them directly) and letting others access that information only if they ask through a provided mechanism we will call method. The mechanisms for asking an object to reveal information about itself we may call getter.
Having others ask for their sensitive information through a method instead of letting them access directly their personal info allows objects to respond only when they want (to objects they trust, for example) and allows them to hide information they would rather not reveal. If one of our party goers have objections to revealing her age, for example, she may choose to lie about it and tell everyone she is 20. Our objects can let others know their name through a getter.
Some times objects will want others to be able to suggest changes to their own state. One of our objects may, for example, ask others for suggestions as to which color she should die her hair next. For this kind of situations they can declare a setter mechanism through which others can suggest such changes but our objects will still be in control of whether they make those changes or not or whether they will take some other actions triggered by this request. This way our object can decide whether to die her hair green or wait for a better suggestion.
Lets implement this
To achieve this:
* Declare the variables of a class as private.
* Provide public setter and getter methods to modify and view the variables values.
Step 1:
public class GetterSetter{
private String name;
private String idNum;
private int age;
public int getAge(){
return age;
}
public String getName(){
return name;
}
public String getIdNum(){
return idNum;
}
public void setAge( int newAge){
age = newAge;
}
public void setName(String newName){
name = newName;
}
public void setIdNum( String newId){
idNum = newId;
}
}
Then we can access the variables of GetterSetter class as follows:
public class TestSetter{
public static void main(String args[]){
TestSetter testsetter = new TestSetter();
testsetter.setName("Mary");
testsetter.setAge(20);
testsetter.setIdNum("12343MS");
System.out.print("Name : " + testsetter.getName() + " Age : " + testsetter.getAge());
}
}