4

Possible Duplicate:
Why use getters and setters?

Can anyone tell me what is the use of getter/setter method?

In one of the interview someone asked me to write a simple class...so, I wrote the class as shown below:

class X
{
    int i;
public:

    void set(int ii){ i = ii;}
    int get(){ return i;}
};

Now, The question goes like this:

Q1. Why did you declare the variable 'i' as private and not as public?

My answer: Bcoz, data is always sensitive and by declaring it private you'll prevent it from being exposed to the outside world.

To counter my reasoning, the interviewer said..."Then, why did you provide a public getter/setter method?? Since, these member functions are public, so still variable 'i' is exposed to outside world and anyone can change the value of 'i'."

Further, The interviewer suggested me to re-write my previous class as follows:

class X
{
public:

    int i;
};

The benefit would be: you can modify the value of 'i' more quickly...since there is no function calling required this time.

The interviewer's reasoning seems good though....because by making private variable 'i' and public get/set method, you cant protect your data. So, Why do we write get()/set() method ?

Community
  • 1
  • 1
Jatin
  • 1,857
  • 4
  • 24
  • 37

5 Answers5

3

The purpose is that you can always change the setter and the getter method with more sophisticated logic, if needed (for example validity check). If you don't use getter/setter you must write them and then change the code everywhere you change the field, which can lead to errors which will be very hard to find.

Dani
  • 444
  • 1
  • 3
  • 10
3

Having accessors and mutators hide the logic used to access that variable. IF the logic changes you only have to change it in the member function rather than everywhere that access that public variable. This helps keep the internal state data hidden from the outside world so you don't have to worry about the implementation details of those operations.

Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
1

we use getter/setter and private data variables to ensure Data Encapsulation

subodh1989
  • 696
  • 6
  • 13
  • 40
1

well, you could make those accessors private. the client does not need to know the type's data representation -- this is a component of abstraction.

justin
  • 104,054
  • 14
  • 179
  • 226
0

What I answerred is:

I agree that we can still declare variable 'i' as public and we can remove the usage of get/set methods. But This can only be done when you are 100% sure about that you're not going to change your class variable in future.

because...if we remove get/set methods, then we will be using 'i' directly in 100's of files.[like: object.i = 10;] And if by any chance, your variable 'i' changes to 'j', then your files will start failing.

So, to overcome this issue....we use get/set methods. and the only place you need to make change is within the class X, make 'i' --> 'j'.

That's the only use i could think of get/set methods.

Jatin
  • 1,857
  • 4
  • 24
  • 37