Kees Lasser
Kees Lasser

Reputation: 121

What is the difference between a class and an object?

i am learning UML and I need some clarity about class diagrams.

for example I have the class person with firstname, surname, date of birth as attributes.

what are or is the object? i do understand the class (that is person including the attributes), i do understand the attributes. but they are also talking about objects in my book, what would that be?

thank you in advance.

Upvotes: 3

Views: 362

Answers (2)

Christophe
Christophe

Reputation: 73376

I like qwerty_so's well documented and comprehensive answer. May I suggest an intuitive alternative?

  • A class describes features of the objects that belong to the class. For example, the class Person defines firstname, surname, dateOfBirth as properties. It also defines the type of these properties.
  • Object of the class Person, say John:Person or Jane:Person have specific values for each of the properties. The object John would have firstname="John", lastname="Doe", dateOfBirth=1961-10-01, whereas the object Jane would have firstname="Jane", lastname="Smith",dateOfBirth=1965-02-20.

The same difference can be experimented with in popular programming languages. Example:

// definition of a class, i.e. the general rules
class Person {
  private String firstname; 
  private String lastname; 
  ...
  public Person (String first, String last, ...) { ... } 
  public void doSomething() { ... } 
}

// definition of objects, that abide by the classe's rules: 
Person Jane = new Person("Jane", "Smith", ...); 
Person P2 = new Person("John", "Doe", ...); 
// the objects need to have their properties set.  But the behaviors of the class can be used without redefining them
Jane.doSomething(); 

Upvotes: 1

qwerty_so
qwerty_so

Reputation: 36315

Where to start? Not that easy. Trying to find the definition of Class in UML 2.5 PDF is like looking for something in the remains of the Twin towers after 9/11 :-( So I took the UML metamodel published by OMG as XMI and imported that into EA. And there were are:

Class

You find the element like this:

enter image description here

and it's comment reads

A Class classifies a set of objects and specifies the features that characterize the structure and behavior of those objects. A Class may have an internal structure and Ports.

So, as it looks, a Class is derived from objects. Pretty much what Carl Linnaeus did in the 18th century. We can leave that for the moment and start looking for those objects.

Object

Trying to look for an Object element in the metamodel revealed: nothing. Probably for good reason since it's going into metaphysics. And Carl from above wasn't the only guy thinking about classification of the world.

Side note: I created an instance of Class in EA and ended up with an element of the metatype Object. A relic from pre UML 2.0 times. I looked into UML 1.5 and actually found a definition of object on p. 3-64:

3.39 Object

3.39.1 Semantics

An object represents a particular instance of a class. It has identity and attribute values. A similar notation also represents a role within a collaboration because roles have instance-like characteristics.

This has settled for a long time and is still in the mindset of most people. The IT guys defined a couple of classes out of the blue (with some requirements in mind) and when you use them you have these objects. Quite the other way around our Carl was approaching things. And now 12 years after UML 1.5 we have UML 2.5 and no trace of Object!

So, per UML 2.5 an Object does not exist. But we have ObjectFlow and ObjectNodes. So there must be something. On p. 12 of UML 2.5 you find

6.3 On the Semantics of UML

6.3.1 Models and What They Model

Classifiers. A classifier describes a set of objects. An object is an individual with a state and relationships to other objects. The state of an object identifies the values for that object of properties of the classifier of the object. (In some cases, a classifier itself may also be considered an individual; for example, see the discussion of static structural features in sub clause 9.4.3.)

and a bit below

UML models do not contain objects, occurrences, or executions, because such individuals are part of the domain being modeled, not the content of the models themselves. UML does have modeling constructs for directly modeling individuals: instance specifications, occurrence specifications, and execution specifications for modeling objects, occurrences, and executions, respectively, within a particular context.

Honestly I was surprised to read that, since I still live in the past where we had object diagrams (UML tools like EA still allow to create them). And that's probably the cause of the confusion. An object is by far too complex (and it has been in discussion since the invention of philosophy) to end up as UML element. Instead, UML allows to bring light to certain aspects of an object by using e.g. SDs to see details of behavior.

Summary

It's a bit of the hen and egg problem. Mapping between real world (the "Objects") and what has been modeled (the "Class") is tough. And it depends on your goals. Are you trying to get your head around somehing that already exists and sketch its behavior or is it that you have invented something new where you want to see how it interacts?

In any case, your question, so simple it is, turns out to be an excellent one!

Upvotes: 2

Related Questions