Chapter:Objects - Relation to classes

From Juneday education
Jump to: navigation, search

Meta information about this chapter

Expand using link to the right to see the full content.

Purpose

This course material teaches the concept of objects before a deeper look at the concept classes. This is a decision made by the authors. However, it is a classic dilemma when teaching object oriented programming languages, that when you ask someone who knows object orientation to explain objects and classes, it is very tempting to explain these closely related concepts in terms of each other. This must be avoided as far as possible or at least addressed in some way.

In fact, we've seen in teaching materials and literature circular definitions like these:

Objects: Entities created from classes, which define the objects' common treats.

Classes: The blueprints describing the common treats for objects.

Now, the problem with definitions such as the the ones above, is of course that it is confusing for students when two related concepts are defined in terms of each other. Another problem with the definitions above is that they are not complete. Arrays are objects in Java, but they are not defined in classes. And there are plenty of classes which are not blueprints for creating objects. Think about classes with only static members such as Math or System, or a class which only declares a main method. No blueprints for objects there!

Now, if we can't define objects in terms of classes alone, we can focus on what objects can do and how to use them programatically, and syntactically. But at some point, we must create objects, e.g. calling a constructor, when showing how objects are used. At this point, it is almost impossible to avoid mentioning classes.

This chapter serves as a soft introduction to classes and their relation to objects. We explain that classes are a later topic for the course material and that we only need to know that they exist and they have a relation to objects. We don't need to know how to write classes just yet, since we are focusing on objects. And there are plenty of predefined classes shipped with the SDK in Java's API.

So the purpose of this chapter is to give a first simple explanation of what a class is, from the point of view of an object.

Goals

The goal of this chapter is to give a simplified first notion on what a class is, and what the relationship between objects and classes are on a high abstract level.

Instructions to the teacher

Common problems related to this chapter

The only problem we've seen with this chapter is that it is hard not to go too deep into classes, which means leaving the focus on objects. Try to be brief and to keep the introduction of classes on a high abstraction level, and refer to later lectures in the course for details on how to write classes and what classes can be outside the scope of being types and blueprints for objects.

Objects' relation to Classes - a short introductory video

We'll give you a short introductory video (see link below under "Videos") instead of text for this chapter.

This video lecture is just a short introduction to classes, so that readers at least have some information on the relation between classes and objects before moving on. Classes (writing our own classes) is a topic for later chapters in which we will go into more detail about the writing of classes, their different contents and syntax.

Programming in Java is mostly about objects. Objects are creatures that live in the memory when the program is running. An object contains state (data) and has a set of behaviors (methods) which can be invoked by other objects. We use objects in Java code via reference variables. If we have an object representing a Message in a messaging application, the state could be

  • who the message is for
  • the subject of the message
  • the body of the message.

The behaviour of the object could be

  • send()
  • save()
  • cancel()

Our programming code would interact with the object using a reference variable. The object still resides in the memory of the application and shouldn't be thought of as being stored "inside a variable". Rather, the variable will "refer to" the object (at its place in the memory).

Now, what have classes got to do with all of the above? Classes are units of code we write (or use if we find useful classes someone else has written) which describes objects type. We say that, for instance, two distinct Message objects "belong to the same class" and the class would typically be called Message (in this case). The reference variables are declared with the Class name as the declaring type. To declare a variable of the class Message (if we imagine there is such a class), we would write:

Message messageToSend;

The reference variable will be called messageToSend and its type can be described as "reference to object of type Message".

If our application was to deal with cars instead of messages, we'd likely need a class Car describing Car objects. Or if we want an address book to go with our messaging application, we'd probably end up writing and using a class Contact (for objects representing contacts we can send messages to using the application).

In this short introduction, we'll settle with saying that classes define the type of objects which share the same type and set of data and the same set of behaviors. Like we said above, a lot more on classes later on in this book!

Chapters about Objects

Here are the chapters about objects in this book:

Videos

Links

Further reading

Where to go next

Next page has exercises for this chapter: Objects_-_Relation_to_classes_-_Exercises

« PreviousBook TOCNext »