Objects, classes and references

Introduction

While marking some second-year assignments last year, I found that a number of students were still having trouble with the ideas behind classes, objects and references. These are important concepts, and without a good understanding of them you will find programming difficult. I have found in the past that a different point of view can help - a look at what classes, objects and references really are inside the computer’s memory. You’ll see that the basic ideas are very simple.

These web pages shouldn’t take long to read, and you can come back to them whenever you feel confused about object-oriented programming. There are also some short quizzes to help engage your brain and to let you know if your understanding is correct.

The pages

Now, the pages themselves. The first four pages describe objects, classes and references. The other pages show examples of real code which link objects together in interesting ways.

What are objects and classes?

What are objects? What problems do they solve? How are classes and objects related?

References

What are references?

References 2

References inside objects

References quiz

Some questions on references

More pages

These extra pages are useful once you fully understand how objects, classes and references work. The first gives you a another way of changing object functionality instead of using inheritance. The "logging" pages work through some real code, building a logging system using different techniques.

Favour composition over inheritance

Use 'has a' instead of 'is a' relationships

Logging, Part 1: Creating links between objects

An 'error logger' from an old project

Logging, Part 2: Changing behaviours with abstract classes

Changing what the logger does by making it abstract and using subclasses

Logging, Part 3: Using composition instead of inheritance

Changing what the logger does by plugging in an output object