• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is object?

 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for help.
Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic