Search code examples
classobjectvariablesattributesinstance

Difference between objects, attributes, variables and class instance


I am having trouble understanding my professor's lecture notes because my brain seem to treat objects, attributes, variables and class instance as interchangeable. I really appreciate any help in distinguishing these 4 terms. Thank you!


Solution

  • You don't mention the programming language at question.

    Usually a class is a model or template that declares how a certain category of objects look like.

    You give a class a name and you mention if it inherits members from another class or not.

    You define also the class members. These can be variables that hold data (object state) and methods (class defined functions) that define the object behaviour.

    When you instantiate a class using the declared model , you get an object, that is a concrete class instance. This is a concrete entity, think of it as a new variable in memory, whose data type is the class (instead of for example integer or string data types), whose value is its state in a defined moment in time (the state being the combination of all of its data member variables values at that moment). This object has to have an identity, because it exists in memory and it is a different entity from the other objects you can instantiate from this or any other class. The data member variables hold specific values for each instance. These are not shared between instances.

    Now the member methods can be shared between instances because they have no state, so they are equal for every object. They are called with some arguments and they do some action that changes the object state, or is at least tightly related with the concrete object. But they are common to every object. The methods usually know what concrete object they act upon by means of a special name like 'this' or 'self', that references to 'itself'.

    Objects are usually assigned to variables upon creation, storing a reference to its identity that allows the remaining code to manipulate them.

    You use these variables to refer to the concrete object outside the code of the classes, and use 'this' or 'self' to refer to it from inside the classes.

    Frequently you access object members qualifying with the object name. Like in 'player.run()', or 'player.total_score'. That is if player is a variable to which you assigned a class Player instance. This can look like player = new Player or player = Player().

    Attributes is just another name given to data members. Sometimes attributes and also methods can be public or private, meaning code outside the class can use them, or only the class code can have access.

    Sometimes you see data members or attributes referred as properties. When you access an attribute, you are accessing a property. In some languages like Python, property can mean something a little different but close related anyway...

    Now also depending on the language things can be like described (C++, Java) or you can have everything being treated as objects, including the class definitions (Python).

    You should also search the internet or SO about inheritance, overriding, class diagrams, and other things class related.

    This is all no more than the ability of defining your own data types beoynd the language builtin types.

    You can think of variables as names for boxes (memory containers in a certain address) holding values. But sometimes you want to manipulate not the values but the addresses themselves. This time you say you have references (to addresses). Sometimes variables are just names for those references. References are also known as pointers. But you could do math with pointers (increment, decrement, add a fixed value to...) that you usually don't do with references.