7

From the GoF book:

Prototype is particularly useful with static languages like C++, where classes are not objects, and little or no type information is available at run-time. It's less important in languages like Smalltalk or Objective C that provide what amounts to a prototype (i.e., a class object) for creating instances of each class. This pattern is built into prototype-based languages like Self [US87], in which all object creation happens by cloning a prototype.

What is meant by "classes are not objects"?

Isn't that always the case?

What does it mean for a class to be an object?

StackExchange123
  • 1,871
  • 9
  • 24
  • 4
    The same way a blueprint is not a building. In C++, a class is a blueprint. Instances built based on that blueprint are the actual objects. – WhozCraig May 26 '20 at 02:33
  • It means a class defines what may become an instance, that is, an object, in terms of data types, capabilities, etc. In OOP, objects are like the actionable exemplars of a concept. – Adrian M. May 26 '20 at 02:35
  • You may have a Class `foo` with 1000+ instances (object). – Louis Go May 26 '20 at 02:36
  • 1
    I've updated the question. I thought a class is always not an object but seems that I'm wrong. I was confused why such a thing is mentioned. – StackExchange123 May 26 '20 at 02:38
  • For example in Java you may have an instance of [Class](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/Class.html). That instance is an object. As a language where classes are also objects, Java is _not_ particularly useful with the Prototype design pattern. – jaco0646 May 26 '20 at 03:29

2 Answers2

8

In some languages, when you declare a class, the language-runtime creates an object in memory to represent that class and its properties; you can then call methods on that class-object to find out properties of the class or create objects of that class, and so on.

C++ doesn't have that feature (largely because C++ is designed to minimize runtime overhead); there is no object representing the class. (The closest it comes to that is RTTI's type_info object, but that is really just an object containing some information about the class, and not a full representation of the class itself)

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • Good insight. In what language (that uses classes not prototypes - I assume you mean) does the runtime create an object (and consume memory) to represent the class, before/without instantiating it? Or did I misunderstand your point? – P2000 May 26 '20 at 03:21
  • The quote in the question mentions Smalltalk, Objective C, and Self... and IIRC JavaScript does so as well. (Regarding *when* the class is constructed, i.e. on-demand or at program startup, I don't know; I don't think that detail is considered particularly important, as long as the class-object is available when the code wants to consult it) – Jeremy Friesner May 26 '20 at 03:24
  • 3
    Python and JS are good examples, where you can significantly alter the behavior of the class during the execution by interacting with the class object. – Frax May 26 '20 at 03:53
2

What is meant by "classes are not objects"?

Exactly what it sounds like. In some languages, classes themselves are also objects that you can send messages to. For example, in order to create an instance of a class (i.e. a new object), you send the +alloc message to the class (and then you typically send the resulting object an -init message:

Foo *newFoo = [[Foo alloc] init];

Isn't that always the case?

No. See above. See also Is class an object in object oriented language and Are classes objects in Objective-C?. Examples besides Objective-C include Smalltalk, Scheme, and Dylan.

What does it mean for a class to be an object?

It means that you can work with a class much as you would any other object. Details vary depending on the language. In Objective-C, a class is an object because it's an instance of the Class meta-class. Objective-C makes a distinction between instance methods, i.e. the messages that can be sent to an instance of the class, and class methods, i.e. the messages that can be sent to the class itself. For example, it's very common to have a shared instance of a class and a class method that gets that shared object:

NSFileManager *fileManager = [NSFileManager defaultManager];

Notice that we're not actually allocating an object here, just asking the class for the shared instance (which the class manages) that may or may not already exist (if it doesn't, the class generally creates it).

Caleb
  • 124,013
  • 19
  • 183
  • 272