Can any one explain what the getters and setters method do?
Get-methods and set-methods help in encapsulating data. That's all. Instead of writing
object.wheel = new Wheel(5);
// ...
object.wheel.rotate();
you do
object.setWheel(new Wheel(5));
// ...
object.getWheel().rotate();
This gives you better control of the update of the field. You could for instance:
- Throw an
IllegalArgumentException
if the wheel doesn't fit.
- Compute or load a new wheel on the fly in the
getWheel
-method.
- Let other object listen for wheel-updates
etc.
where we must use getters setters method and how it differ from the normal method?
You don't have to use getters and setters, it's just good practice.
Technically speaking getters and setters are no different from normal methods. They just have a specific (simple) purpose.
And i saw some coding having only setter method so here why getters method not declared?
The author of the class simply didn't want to expose the wheel-object to the user. The reasons for this may vary.