Object Syntax
Objects fulfill a common need in software development: they put data and the code that manages it together in a computational organ. However, each language provides different syntax for forming objects. The most common syntactic device for defining objects is the class. Let's examine the syntax for classes in several mainstream languages.
In C++, which popularized object-oriented programming in the 1990s, an object's state and behaviors are placed in the public and private sections of a class. We might define TreasureChest
like so:
class TreasureChest {
public:
Item open() {
remove();
return item;
}
void remove() { /* ... */ }
private:
Item item;
Point2 position;
};
An object's state should generally be private to prevent outside clients from accessing or modifying the state in unexpected ways. Any behavior of an object may freely access its state and other behaviors, whether they are public or private. Here the open
method accesses item
and remove
, and they are accessed without qualification. Alternatively, we may need or want explicit qualification using this
:
Item open() {
this->remove();
return this->item;
}
In C++, this
is a pointer to the current object. The object used to call the method is implicitly bound to this
. Outside the method the object may be known by a different name or it may be anonymous:
loot.open(); // this <- loot
new TreasureChest().open(); // this <- new TreasureChest()
In Java, classes do not have public and private sections. Instance variables and methods are individually made public or private, as seen in this version of TreasureChest
:
class TreasureChest {
public Item open() {
remove();
return item;
}
public void remove() { /* ... */ }
private Item item;
private Point2 position;
};
Like C++, Java uses this
to refer to the implicit invoking object. However, it is a reference rather than a pointer.
In other languages, the invoking object must be declared explicitly as the first formal parameter. The parameter is usually given the name this
or self
. Unlike in C++ and Java, all references to the state or other behaviors must be qualified by the parameter. This class follows the Python convention of using self
:
class TreasureChest:
# ...
def open(self):
self.remove()
return self.item
The formal parameter suggests that you need to pass the invoking object as the first actual parameter, as in open(chest)
. But this is not how it's done in most languages. The object is considered to be of primary importance and is listed before the behavior: chest.open()
. This ordering reflects that open
is not some function currently in scope but rather a behavior of chest
. It also forms a subject-verb phrase that you commonly encounter in English.
The object left of the .
or ->
operator is the receiver. This term comes from Smalltalk, one of the earliest object-oriented programming languages. In Smalltalk, invoking a behavior on an object was described as sending a message to a receiver.