Is a class an object in an object oriented language

Is a class an object in an object oriented language? How to access class methods only by name class.method name ? (inner work). Is it the same as object.method ?

And if the Class is the same as an object (belongs to the class of the object, which is a superclass of every thing in OO), and we create an instance of it (we make an object from it), we can make an instance of the class, different from the Object class.

(Mainly interested in a theoretical perspective , even if it is practically not required)

+8
source share
4 answers

Well, it depends on the language you use; in pure OO languages, where everything is an object (e.g. Smalltalk ), classes are no exception and are objects. In other languages ​​where classes are not considered first-class citizens, they are simply special language constructs or primitive types. From now on, I will use Smalltalk as the target language because of its support for reflection and uniform style.

Access to class methods is carried out only by the name of the class name .method? (inner work). Is it the same as object.method?

Because classes are objects, they are in turn instances of the class (a metaclass ). Thus, sending a message to a class simply sends a message to an object whose role should represent class behavior. There is a lot of literature, you can look here here and here for acquaintance.

And if the class is the same as the object (belongs to the class of the object that is the super class of every thing in OO) and we create it (we will make the object out of this), can we make an instance of the class different from the Object class.

I'm not sure that I am following you here, but just to clarify, it does not always happen that Object is the superclass of all classes. The fact is that if you start to monitor the relationships between classes and metaclasses, you can achieve a kind of infinite loop. Different languages ​​work differently and, for example, in VisualWorks Smalltalk, Object is a subclass of nil . The fact is that nil also an object (remember that everything is an object) and actually represents "nothing." As you would expect, nil is an instance of the class ( UndefinedObject ), and also implements some of the class's protocols. As a result, it can be used to represent the form of a class where nothing is inherited :).

Finally, I don’t know if this answers your question, but yes, you can do many cool things with full reflective ability, for example, create new classes on the fly or modify existing ones. I will leave you here some documents that may seem interesting on this topic:

+6
source

A class is not an object; you can think of it as a "plan" for the object. It describes the shape and behavior of this object. Objects are instances of a class.

See here for more details.

+3
source

Is a class an object in an object oriented language?

The concept of a class is primarily theoretical. You can define a thing with class semantics, even if your language does not support the formal concept of “class” (for example, Javascript). A class is a template for the object from which you instantiate.

How to access class methods using the class name .method?

I do not quite understand what you mean. Some languages ​​support “static” or “cool” methods, which are methods that are not called in the context of an instance of the class. So this is not the same as “object.method”, which is the usual method in the class where 'this' (or the equivalent) will be the instance on which the method is called.

Java (from comments)

For Java, there is a class called Object and a class called Class . See this . These Java constructs are different from the concepts of class and object . The first implementation details are how designers built the language, and the last are general concepts. So in Java you can have an instance of an object, where the instance is an object (concept) of type Object (Java constructor).

+1
source

The basic concept of an object means that some memory has been allocated. Like devdigital, the specified class is simply a plan for an object that contains a bare-bone structure and determines how the object will behave when it is created.

Classes are just bodies (without a soul), and objects are living bodies (having a soul) that interact with the environment or biological terms, respond to external stimuli (social methods and properties) by analogy with the philosophy of life :)

Technically, classes are just machine-interpreted code in memory (optional main memory or registers). Objects are loaded into executable memory (registers / main memory)

0
source

All Articles