This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 6: Object methods

Methods and attributes

face Josiah Wang

You have probably figured this out by now - you can define your own methods for your own class.

Class diagram with methods

Methods are often actions, so like functions, they will often end up being verbs. For example, a person can speak, walk, run, study, or cry.

In object-oriented programming, attributes represent an object’s state or properties (what an object has), while methods model the object’s behaviour (what an object can do).

An object’s method is bound to the object itself. Object methods have access to the object’s own attributes or states, and can even modify its state (think mutator methods like list.append()). In short, an object’s states and attributes are now ‘in sync’, and acts as a single coherent unit!