BubbleTree
BubbleTree

Reputation: 606

When to make instance variables private?

I'm trying to understand the usage for getter/setter methods in a class. Let's say we have a class called A with some public instance variables followed by a constructor with parameters where arguments were passed from another class(main) to it. Inside the constructor we let those instance variables equal what was passed.

Now if this class were to be used by another programmer, nothing would stop them from directly accessing/changing the instance variables to something that isn't valid. By making the instance variables private we can eliminate access to those variables. However if we wanted to have those instance variables updated/changed indirectly or under some specific condition or perhaps just letting the person have access to the instance variable, we would create a getter/setter pair for this purpose.

Benefits?:
1.Change instance variable only under certain valid reasons under the set() method
2.So that we can show what the instance variable actually is without giving the programmer who is using this class the ability to change it.

Is this a correct interpretation?

Upvotes: 3

Views: 3100

Answers (4)

Nagarjuna Annu
Nagarjuna Annu

Reputation: 1

if an instance variable is to be used only by methods defined with in its class, then it should be made it as private.If an instance variable must be within certain bounds, then it should be private and made available only through accessor methods[getter as well as Setter] Methods.

Upvotes: 0

Anton Bessonov
Anton Bessonov

Reputation: 9813

Yes, your interpretation is correct. But it's because limits of language. For instance in python you don't need write everytime getter or setter, because you can override behavior of member variables. For example:

class MyClass:
    def __init__(self, myproperty):
        self.myproperty = myproperty

And if everybody use it in way like:

print(new MyClass("test").myproperty)

you can still change behavior of you getter:

class MyClass:
    def __init__(self, myproperty):
        self._myproperty = myproperty

    @property
    def myproperty(self):
        return self._myproperty + " changed behavior"

or even of setter without touch code what use you class:

   @myproperty.setter
   def myproperty(self, myproperty):
       if len(myporperty) > 0:
            self._myproperty = myproperty

See property reference for better example.

Upvotes: 0

Maxim Shoustin
Maxim Shoustin

Reputation: 77910

Encapsulation – refers to keeping all the related members (variables and methods) together in an object. Specifying member variables as private can hide the variables and methods. Objects should hide their inner workings from the outside view. Good encapsulation improves code modularity by preventing objects interacting with each other in an unexpected way, which in turn makes future development and refactoring efforts easy.

enter image description here

Being able to encapsulate members of a class is important for security and integrity. We can protect variables from unacceptable values. The sample code above describes how encapsulation can be used to protect the MyMarks object from having negative values. Any modification to member variable vmarks can only be carried out through the setter method setMarks(int mark). This prevents the object MyMarks from having any negative values by throwing an exception.

Upvotes: 7

Rich O'Kelly
Rich O'Kelly

Reputation: 41767

Your interpretation is correct. Also (off the top of my head):

  • It allows the implementation of the class to change (eg if you wish to remove the field and replace it) without forcing consumers to interact with your class any differently.
  • It allows AOP frameworks to intercept calls to your get / set method.
  • You can specify permissions via annotations for access to methods.

Upvotes: 1

Related Questions