Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDervin Buckery
425 PointsBehavior vs State
I am having trouble understanding what exactly state and behavior is. Can someone please explain and provide an example or reference so I can gain an better understanding?
Please and thank you!
1 Answer
Justin Horner
Treehouse Guest TeacherHello Dervin,
The example I often use to describe State and Behavior of objects is a vehicle. A vehicle has state that it can indicate to you, like how much gas is in the tank. As the driver, you may make a decision based on the state, like knowing when it's time to stop and refill at the nearest gas station.
Then the car has behavior that you control as a user, like acceleration. You instruct the car to accelerate using the pedal, but it's a behavior of the car to accelerate. This would be in the form of some function/method of the class like Accelerate();
.
Which brings us to another form of state - is the car moving/accelerating? Again, this is something that an object could provide as a property to let you know the current state of the car movement.
I hope this helps.