i have studied that " getter and setter methods in real world programming are the best examples for encapsulation ".,
what it actually means??
"as private member variables can't be accessed outside of our class, so with the getter and setter methods we can assign them values and access them " i am thinking in this way., am i correct?
can anyone provide me example to explain clearly explaining Encapsulation (which is an important feature of obj oriented prog) with and example and with setter and getter methods supporting that feature.,
This is just a simple example. In a more complex program you could validate the name parameter in setName if it matches to certain criteria.
Or you could hide certain properties of an object and make others accessible.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Joined: Oct 26, 2008
public class Speed
public void setSpeed(int s)
if (s<= 0)
System.out.println("sorry..! you can't assign a null or negative value as speed ");
speed = s;
public class SpeedTest
public static void main(String  args)
Speed spd = new Speed();
// can it be the valid code for explaining as an example for encapsulation in the java technical interviews?
// please reply me and suggest me .,