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 trialGerry K
5,877 PointsClarify what is state and behaviour of an object
I don't fully understand what these two aspects of an object are and feel it wasn't well explained in the video so was hoping someone could clarify with an example of what these two things actually mean.
Sean Flanagan
33,235 PointsHi Gerry K
Imagine a dog. This is an example of an object.
A state can be on or off. If it's asleep, its state is off. If it's awake, the state is on.
Behaviour: If the dog is awake, what is it doing? Examples of behaviour might be barking, sniffing, eating or drinking, pawing, jumping, playing, to name a few.
I hope my explanation makes sense.
1 Answer
jlampstack
23,932 PointsIn simple terms, the state of an object is its state at a certain point in time, and a behaviour of an object is the things it can do which is stored in methods. When the method is run, it performs an action, thus changing the state of the object.
State Examples:
- A radio can be "on" or "off"
- A song can be "playing" or "not playing"
- A task can be either "complete" or "incomplete"
Behaviour Examples:
- If a power button is pressed, the radio turns from "off" to "on"
- If a stop button is pressed, the song that is playing "stops"
- If you finish a task and click on the checkbox to the left, a line crosses thru the text task indicating it's complete
Ken Stone
29,703 PointsKen Stone
29,703 Pointsfor the radio example state is like on or off, the radio could be on so the state would be on behavior is what is does, it plays music, when the state is off the behavior is not playing music
another example could be a cow the state could be sleeping or awake when the state is awake the behavior could be eating grass