This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
What about the Getters and Setters for the rest of the properties? Do you have to add Getters and Setters for ALL of them? What if you donβt care about doing anything with the property before it gets set. Can we just leave those public?
Documentation
Why Getters and Setters
Some of these are concepts we wont be covering in this course, but here are a few ides:
- Controlling the of behavior associated with getting or setting the property - this allows additional functionality (like validation) to be added more easily later.
- Hiding the internal representation of the property while exposing a property using an alternative representation.
- Allowing the getter/setter to be passed around as lambda expressions rather than values.
- Getters and setters can allow different access levels - for example the get may be public, but the set could be protected.
- Insulating your public interface from change - allowing the public interface to remain constant while the implementation changes without affecting existing consumers.
- Providing a debugging interception point for when a property changes at runtime.
- Improved interoperability with libraries that are designed to operate against property getter/setters - Mocking, Serialization, etc.
- Allowing inheritors to change the semantics of how the property behaves and is exposed by overriding the getter/setter methods.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up