views:

104

answers:

7

Why don't make all variable private? All the variables must give a getter, setter to modify/read. Why leave the "public" keyword here? Except for convenient, any another reason for that?

A: 

Making a variable private is called information hiding or encapsulation. This concepts are introduced to separate program logic to program implementation.

Having a variable public or having a variable private with public getter and setter is not the same, even the getter and the setter doesn't make any transformation to the variable. Getter and setter are made to do some kind of operation on the variable when this is set or it is get, like caching or constraints check.

A public getter, or setter, could be overwritten in a subclass, specializing its behavior. You cannot do the same with a public variable. Even with properties the behavior is not the same because you cannot overwrite an accessor in a subclass. Writing (empty) accessors gives you more extension possibilities.

Note that this concepts are not implemented by all languages for example C and Python they don't.

mg
+4  A: 

Well, they do have a default.

The question you should ask yourself is why not everything public, once you understand that - I'm sure you can answer your own question.

Stustu
A: 

If you are the only person who use this code - make it all public. If not - you should use private fields if you don't want somebody to change it because this may ruin your design and behaviour of your class will be unpredictable.

Hun1Ahpu
A: 
  1. Some variables are needed for internal logic only
  2. Possible verification on setting new values. If you don't need it know, probably you'll need it later
Eugene
+1  A: 

You are very right, it's better to have the variable private, and have public getter/setter. Direct access to public field is actually a bad practice.

This relates to what is called "representation independance": that fact that if external access happen through getter/setter you can change the internal representation of the object without major glitches.

ewernli
+1  A: 

Truth is that not a lot would be lost if fields were forced to be private in a language. In most langauages, like you say, the recommendation is to encapsulate all fields with a property setter/getter. A language could quite reasonably force all fields to be private.

However, if you have something like a private inner class:

public class OuterClass
{
     private InnerClass
     {
          public int publicField;
     }
}

Then you may argue that getters/setters are not required. In this case, in C#, it is just as easy to use an auto-implemented property. However these were only introduced in C# 2, so I guess this is the reason that public fields are still allowed. Pre-auto-implemented properties, one would have had to write getters/setters, which may be considered overkill for a private inner class.

In languages which have pointers, such as C++, public fields are required if one wishes to obtain a pointer to a field from outside the class.

Groky
A: 

The usual reflex for a programmer is to set all fields in a class as private* and offer an interface formed of a series of getters and setters to manipulate them. This is according to the encapsulation principle where the inner workings of a class remain hidden and allows the programmer to state what changes are possible.

The public, protected and default (=none) access specifier are needed in some cases. Public can be used for any method or field that should be freely accessible from outside a class while the absence of a specifier limits access to the class itself and any class inside the same package. Finally, protected can be used in when fields need to be accessed from subclasses and other classes in a package which is an approach sometimes used in frameworks.

Here's a link showing the differences between access specifiers:
http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

*With the exception of container objects since retrieving an instance gives you full control.

James P.