You need to sign in to do that
Don't have an account?
EdCode
Basic concept/question: what is the difference between "Attribute" and "Variable"
I have learnt the following about "Attributes" and "Variables". Could you please confirm if what I have learnt is correct or incorrect? (I am a bit confused with these two concepts now).
I am not sure if the above is 100% correct.
I suppose that what 's confusing me is the fact that the way Attributes are defined is similar to the way Variables are declared:
Here I define an attribute (should I use the term "define"? or... some other term?):
Could you possibly help me to clarify the conceptual difference between attribute and variable ?
Thank you.
- Attributes:
- Are the "adjectives" or "parts" of a future object that is going to be constructed with a Constructor. I am guessing that we use the term "attribute" when we are, somehow, "designing" the object that will be constructed in the future.
- Attributes are also called "properties"(?)
- Example: we may be designing the future construction of a car (we design the car with a class "Car" where we specify all of its attributes which will be used by a Constructor which, in turn, will construct objects). When we design/define/specify these "parts" or "adjectives" , then I THINK that thats when we use the term "attributes". Right? (that's when we are still defining how objects are going to be)
- An attribute, I think, is very similar to a Variable; the difference is that the attributes are to be used by a Constructor. Variables are not used in Constructors. Is that Right?
- Variables:
- Variables, I GUESS, are similar to attributes but they only start being called "Variables" when they have been declared as such which means they are capable of storing values. (attributes do not have this capability)
- Once a Variable has been declared as a Variable, it becomes a "ready space in memory", ready to store values.
I am not sure if the above is 100% correct.
I suppose that what 's confusing me is the fact that the way Attributes are defined is similar to the way Variables are declared:
Here I define an attribute (should I use the term "define"? or... some other term?):
mode type of NameOfClass nameOfAttribute;Here below I declare a variable:
type or NameOfClass nameOfVariable;I observe that, when defining an attribute, there is a "mode" (such as "private" or "public") but when declaring a variable, we do not include a "mode".
Could you possibly help me to clarify the conceptual difference between attribute and variable ?
Thank you.
Where you can see that even though we haven't specified a color in the NewCar myCar = new NewCar() line, our class definition automatically assigns the color blue because exteriorColor is an attribute of NewCar and is pre-set to "Blue."
Similarly, if we modify the class to also include interiorColor as an attribute: And then execute this: We get this in the debug log, where now interiorColor appears, albeit as a null value. (It's null because we haven't also called the method, buildCar(), that actually sets a color (this is slightly different from my very first example). All we've done is created a new NewCar object. It knows it has an attribute that is supposed to store the interior color, but we haven't set it yet.)
If, however, you move exteriorColor and interiorColor within the buildCar method, they are no longer considered attributes: and executing results in the following debug log:
Does that help?
All Answers
On your example of code (above), why interiorColor is NOT considered as an attribute? Is it because, interiorColor is not included as a parameter of the constructor?
InteriorColor here could well have been an attribute or a characteristic of the Class itself (you know, interior color is similar to external color, it is a characteristic of the car and it could be part of its blue print).
I need to make sure I undestand the distinction between Attributes an Variables and WHEN/WHY should we use one and/or the other.
Could you possible explain it to me in plain English?
Thank you very much.
Where you can see that even though we haven't specified a color in the NewCar myCar = new NewCar() line, our class definition automatically assigns the color blue because exteriorColor is an attribute of NewCar and is pre-set to "Blue."
Similarly, if we modify the class to also include interiorColor as an attribute: And then execute this: We get this in the debug log, where now interiorColor appears, albeit as a null value. (It's null because we haven't also called the method, buildCar(), that actually sets a color (this is slightly different from my very first example). All we've done is created a new NewCar object. It knows it has an attribute that is supposed to store the interior color, but we haven't set it yet.)
If, however, you move exteriorColor and interiorColor within the buildCar method, they are no longer considered attributes: and executing results in the following debug log:
Does that help?