+ 1
"You may validate the given value in the setter before actually setting the value". Explain this?
Getters and setters allow us to have control over the values. You may, for example, validate the given value in the setter before actually setting the value.
1 Réponse
+ 1
A sample code to set marks:
void setMarks(int marks)
{
if(marks<0 || marks>100)
System.out.println("Invalid value to set); //invalid value entered so don't accept to set
else
this.marks=marks; // else you can set
}