Java Java Objects Meet Objects Methods

Daud Zeb
Daud Zeb
701 Points

Method exposes value or expresses behaviour?

Craig says we use methods to help express the behaviour of objects while he uses method to expose the value of field characterName without letting someone set it. How do these two ideas come together? The behaviour of the PezDispenser would be things like dispensing Pez not exposing its name right?

1 Answer

Hey Daud! This is a good question. I think the issue is your getting a little too hung up on that:

We use methods to help express the behaviour of objects

We have a getName() function simply because the name field is private by default, and so we use a function to get the name. Don't think of that function as necessarily describing an intrinsic characteristic of the object, just as a helpful method.

That being said, you could argue that this, in fact, does describe the behaviour of an object. It would not be unreasonable to suggest humans, as an object, has a getName() method that can be called by asking, "What's your name?".