Forums Register Login

What is object?

+Pie Number of slices to send: Send
Hi,
I have learnt C/C++ before and know how to create an object.
When I read the student guide of the java lang, e.g.
class MyDate {
int day, month, year;
}
MyDate myBirth = new MyDate();
This statement allocates the space for the 3 integers used to form MyDate. Object assignment sets up the variable myBirth to refer to the new object. The contents of the MyDate object can be accessed through myBirth.
Q.1 myBirth is object or variable or they are the same meaning?
Q.2 Mydate is an object or class type or they are the same meaning?
In C++, it is clear that, for instance,
class MyDate {
int a, b, c;
};
main() {
MyDate myBirth; //An object is created.
}
or main() {
MyDate* pmyBirth = new MyDate; //An object is created dynamically.
delete pmyBirth;
}
I am confused with the terminology, MyDate is class in C++. But, MyDate is also an object in Java.
Anyone could clarify it for me?
Thanks
Andrew
+Pie Number of slices to send: Send
Classes and objects are not the same thing. You instantiate an object from a class (the class is the blueprint for creating the object, or the cookie-cutter, if you want to use the common analogy). The object's type is whatever the classname is.
A variable also has a type. However, you don't manipulate objects directly through the variables, the variable instead holds a reference value to an object. The neat thing is that the variable might be of a certain type, and the object that the variable references be of another. For example, a Superclass variable type that really holds a reference to a Subclass object.
Hope that helps a little
Jason
+Pie Number of slices to send: Send
Q.1 myBirth is object or variable or they are the same meaning?

myBirth is a variable pointing to an object of type MyDate.

Similar to C++, an object is created by
new MyDate();
and is assigned to the variable myBirth.

int day, month, year;
are also variables of type int.

Q.2 Mydate is an object or class type or they are the same meaning?

You don't have anything named Mydate.
MyDate is a class. A class defines a type. You create objects of MyDate type (using the keyword "new") and refer to them by reference variables like myBirth.
+Pie Number of slices to send: Send
Thanks for help.
Andrew
Attractive, successful people love this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 734 times.
Similar Threads
Garbage collection-Marcus exam
Pass by value, reference, the this reference and invoking constructor??
Thread's
Thread's
Thread's
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:38:46.