2 added 6 characters in body
source | link

I can think of one use case where this makes sense. You might have a class that originally you access through a simple getter/setter API. You later extend or modify so that it no longer uses the same fields, but still supports the same API.

A somewhat contrived example: a Point that starts out as a Cartesian pair with p.x() and p.y(). You later make a new implementation or subclass that uses polar coordinates, so you can also call p.r() and p.theta(), but your client code that calls p.x() and p.y() remains valid. The class itself transparently converts from the internal polar form, that is, y() would now return r * sin(theta);. (In this example, setting only x() or y() does not make as much sense, but still is possible.)

In this case, you might find yourself saying, “Glad I bothered automatically declaring getters and setters instead of making the fields public, or I’d have brokenhad to break my API there.”

I can think of one use case where this makes sense. You might have a class that originally you access through a simple getter/setter API. You later extend or modify so that it no longer uses the same fields, but still supports the same API.

A somewhat contrived example: a Point that starts out as a Cartesian pair with p.x() and p.y(). You later make a new implementation or subclass that uses polar coordinates, so you can also call p.r() and p.theta(), but your client code that calls p.x() and p.y() remains valid. The class itself transparently converts from the internal polar form, that is, y() would now return r * sin(theta);.

In this case, you might find yourself saying, “Glad I bothered automatically declaring getters and setters instead of making the fields public, or I’d have broken my API there.”

I can think of one use case where this makes sense. You might have a class that originally you access through a simple getter/setter API. You later extend or modify so that it no longer uses the same fields, but still supports the same API.

A somewhat contrived example: a Point that starts out as a Cartesian pair with p.x() and p.y(). You later make a new implementation or subclass that uses polar coordinates, so you can also call p.r() and p.theta(), but your client code that calls p.x() and p.y() remains valid. The class itself transparently converts from the internal polar form, that is, y() would now return r * sin(theta);. (In this example, setting only x() or y() does not make as much sense, but still is possible.)

In this case, you might find yourself saying, “Glad I bothered automatically declaring getters and setters instead of making the fields public, or I’d have had to break my API there.”

1
source | link

I can think of one use case where this makes sense. You might have a class that originally you access through a simple getter/setter API. You later extend or modify so that it no longer uses the same fields, but still supports the same API.

A somewhat contrived example: a Point that starts out as a Cartesian pair with p.x() and p.y(). You later make a new implementation or subclass that uses polar coordinates, so you can also call p.r() and p.theta(), but your client code that calls p.x() and p.y() remains valid. The class itself transparently converts from the internal polar form, that is, y() would now return r * sin(theta);.

In this case, you might find yourself saying, “Glad I bothered automatically declaring getters and setters instead of making the fields public, or I’d have broken my API there.”