Upgrade to Pro — share decks privately, control downloads, hide ads and more …

M250 Unit 2 Subsection 2 - Classes and protocols (6)

Matt
January 06, 2020

M250 Unit 2 Subsection 2 - Classes and protocols (6)

Unit 2 is in many ways just an extension of unit 1. So here we get to continue our gentle approach to Java.

Matt

January 06, 2020
Tweet

More Decks by Matt

Other Decks in Education

Transcript

  1. Initialisation Firstly, recall from Unit 1 that objects have attributes

    with attribute values. These attribute values are collectively called its state and we can also think of state as the object’s data. Initialisation is the setting of an object’s state upon its creation. It is called this because it gives the attributes their initial value.
  2. For example, we can create Frog objects (instances of the

    class Frog). A Frog object has the attributes colour and position. All instances of Frog are created with these attributes set to the same initial values. In the case of Frog objects, these are: • colour is initialised to green • position is initialised to 1
  3. The actual code we write to initialise objects is done

    within the class (and this is explained in later units). Right now we just need to recognise that no matter how many instances of the Frog class we create, they will all begin with the same initial state. It is only through the sending of messages that instances of the Frog class can alter in state.
  4. This unit also introduces instances of the Toad class (Toad

    objects) in addition to instances of Frog. Toad objects also have the attributes colour and position, but are instead initialised to have a position of 11 and a colour of brown. So we can see that Frog objects and Toad objects are both instances of different classes and these classes initialise them differently.
  5. Frog objects and Toad objects have the same set of

    attributes, but are initialised differently. However, it wouldn’t be hard to imagine two instances of different classes that have completely different attributes to each other. For example -- a Pen object with the attribute ink, and a guitar object with the attributes wood and strings – both share no common attributes (as we would expect as they represent completely different objects.)
  6. Frog and Toad objects have different protocols / behaviours In

    addition to being initialised differently, instances of Frog and Toad also have different protocols. We can see that Frogs understand the message jump(), but Toads don’t. So we say that jump() is not in the protocol of Toad.
  7. In contrast to this there are some messages that both

    Frog and Toad objects do understand but which cause different behaviour when they are received. For example, the message home() returns a Frog object to position 1, whereas the message home() returns a Toad object to position 11. Toad objects also move an additional step in response to left() and right() than Frog objects do.
  8. It’s also important to note that many messages cause the

    same behaviour when sent to a Frog or Toad object. The messages they respond the same to are green(), brown(), and croak().
  9. The benefits of using classes Using classes to define objects

    is useful because we only need to write the code to define the object once -- in the class. Then we can make virtually as many copies of the object as we like, each of which holds their own unique data (in the form of attribute values) and can communicate with other objects.
  10. Using classes to define objects reduces code duplication, the workload

    of the programmer, and the chance of introducing errors to the code. Also, if we need to change any attribute or behaviour of a class of objects then we only need to make this change in once place: the class.