S
Scott.R.Lemke
Before I start, I understand the implementation of getters and setters
and how to use them, I'm not asking about how, I'm asking about why.
encapsulating anything:
Class Foo{
private int bar;
public int getBar() { return bar; }
public void setBar(int newBar) { bar = newBar; }
}
The basic principle behind encapsulation is information hiding.
Throwing a layer between the implementation details of the class, with
the public interface, giving me the ability to change the
implementation without changing the interface.
I would argue that the above example, which I am sure everyone here has
written before, many times, not only does not provide encapsulation,
but breaks 2 basic principles without the developer doing it
intentiolly; Never allow your objects to get into an illegal state, I
could introduce an illegal value through the setter, and keep your
instance values private, someFoo.getBar() is essentially an alias for
someFoo.bar.
I may be looking at this the wrong way, it may even be my style coming
into play. To me, a proper getter/setter would be something like the
following:
Class Moo{
private Bubba aBubba;
public BubbaInterface getBubba() { return aBubba;}
public void setBubba(BubbaInterface newBubba) {
if(newBubba.isValidBubba()) aBubba = newBubba;}
}
Here, I am hiding the implementation, as I am dealing in interfaces,
and for all intents and purposes, the user of this class has no idea if
I have a Bubba or some other class that implements BubbaInterface as a
member variable.
I guess my question is manyfold. Is the use of getters and setters on
primitives, or even concrete classes, providing encapsulation. If the
getters and setters do nothing with the in/out values, are they nothing
more than macros? Does a getter that does nothing more than return a
member variable break security?
I'm sure there are more questions in my head somewhere, but this should
be a good start.
and how to use them, I'm not asking about how, I'm asking about why.
to be needed to "preserve encapsulation" But, how is the followingFrom day 1 of ever seeing a Java class, getters and setters were said
encapsulating anything:
Class Foo{
private int bar;
public int getBar() { return bar; }
public void setBar(int newBar) { bar = newBar; }
}
The basic principle behind encapsulation is information hiding.
Throwing a layer between the implementation details of the class, with
the public interface, giving me the ability to change the
implementation without changing the interface.
I would argue that the above example, which I am sure everyone here has
written before, many times, not only does not provide encapsulation,
but breaks 2 basic principles without the developer doing it
intentiolly; Never allow your objects to get into an illegal state, I
could introduce an illegal value through the setter, and keep your
instance values private, someFoo.getBar() is essentially an alias for
someFoo.bar.
I may be looking at this the wrong way, it may even be my style coming
into play. To me, a proper getter/setter would be something like the
following:
Class Moo{
private Bubba aBubba;
public BubbaInterface getBubba() { return aBubba;}
public void setBubba(BubbaInterface newBubba) {
if(newBubba.isValidBubba()) aBubba = newBubba;}
}
Here, I am hiding the implementation, as I am dealing in interfaces,
and for all intents and purposes, the user of this class has no idea if
I have a Bubba or some other class that implements BubbaInterface as a
member variable.
I guess my question is manyfold. Is the use of getters and setters on
primitives, or even concrete classes, providing encapsulation. If the
getters and setters do nothing with the in/out values, are they nothing
more than macros? Does a getter that does nothing more than return a
member variable break security?
I'm sure there are more questions in my head somewhere, but this should
be a good start.