1.1 Introduction to OOP

Introduction

Traditionally, code has been written as sequential instructions, much like a recipe. Code may be grouped into blocks and functions, and flow may be controlled using conditionals, loops and function calls. However, this paradigm is not a natural way for us to think about data.

With Object-oriented Programming (OOP), code and data are grouped together into objects, which interact with each other through 'messages' sent and received through publicly exposed interfaces. For example, a client object can checkout a book object from a library object. As you can see, we can immediately relate to and conceptualize the objects and data involved with this object-oriented way of thinking about programming.

As a further introduction, here is a video simplifying the concepts of objects, data, code and messages.


Classes vs Objects

Many students initially find the terminology a bit confusing as first, especially between classes and objects, as they appear at first to be one and the same.

Classes

A class is simply a class of objects. You can think of a class as a recipe or description of a type of object. For example, the recipe for chocolate cake is not, itself, a chocolate cake, but rather it is a description of a type of cake.

Objects

An object is a specific instance of a class. Following on from our previous illustration, each time you bake a cake following the chocolate cake recipe, the result is a chocolate cake. Even though all cakes are made using the recipe, they are all different in subtle ways, and exist in different locations. Similarly, objects in code are instances of a specific class definition.

In the next section, we will learn how to design classes.


Exercises

Identify the objects and classes involved in:

  1. sending money from one person to another via internet banking.
  2. sending an email to someone in your contact list.
  3. posting a message your Facebook friend's wall.

Continue to the next section


Comments

comments powered by Disqus