Thursday, March 11, 2010

Classes and Instances

The next important principle of object-oriented programming is
All objects are instances of a class. The method invoked by an object in
response to a message is determined by the class of the receiver. All objects
of a given class use the same method in response to similar messages.
Fred is an instance of a category or class of people i.e. Fred is an instance of a
class of florists. The term florist represents a class or category of all florists. Fred is
an object or instance of a class.
We interact with instances of a class but the class determines the behaviour of instances.
We can tell a lot about how Fred will behave by understanding how Florists
behave. We know, for example, that Fred, like all florists can arrange and deliver
flowers.
In the real world there is this distinction between classes and objects. Real-world
objects share two characteristics: They all have state and behavior. For example, dogs
have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail).
Students have state (name, student number, courses they are registered for, gender)
and behavior (take tests, attend courses, write tests, party).

No comments:

Post a Comment