One important thing about casting (up or down) is that it is talking about the type setting of the reference variable, not the object. Take the following examples
1. Implicit casting (also known as promotion, or casting up)
//Son is child class of Parent
Parent p = new Parent();
Son s = new Son();
p = s; //implicit casting
p = (Parent)s; //also okay, but uncessary to do in this way
2. Explicit casting
p = new Son(); //OK
s = p; //error
s = (Son) p; //explicit casting, have to do in this way
3. Wrong use of Explicit casting
p = new Parent();
s = p; //error
s = (Son) p; //also error, no way to use a Son type variable to point to a Parent object
Cliff Chu
-----Original Message-----
From: Emmanuel Eze [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, May 07, 2002 5:43 PM
To: JDJList
Subject: [jdjlist] RE: [jdjlist] RE: [jdjlist] casting
can u give an example of explicit cast?
-----Original Message-----
From: RAMESH VISHNUVARDHAN [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, May 07, 2002 12:18 PM
To: JDJList
Subject: [jdjlist] RE: [jdjlist] casting
When you assign a variable of parent class type to an object of subclass,
the implicit casting occurs.
Example:
java.util.Date parentDate = null;
parentDate = new java.sql.Date(Calendar.getInstance().getTime().getTime());
The above code is valid. Here we no need to do explicit casting
.
Regards,
Vishnu
-----Original Message-----
From: Emmanuel Eze
To: JDJList
Sent: 5/7/02 11:08 AM
Subject: [jdjlist] casting
This cast thing is beginning to get me confused. if I cannot cast a
java.util.Date to a java.sql.Date since the util.Date is the ancestor
class,
can I cast a java.sql.Date to java.util.Date.� I really wish to
understand
the nitty gritty of casting.
�
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm
http://www.sys-con.com/java/list.cfm
