| Author |
Question on casting
|
Veena Pointi
Ranch Hand
Joined: Jun 20, 2002
Posts: 442
|
|
According to R&H book the following are run time rules for object casting. newtype nt; oldtype ot; nt=(newtype)ot; 1.If newtype is class the class of the expression being converted to must inherit from newtype. 2.If newtype is Interface,the class of the expression being converted to must implement that interface. What about the following scenario? When old type is interface & new type is class What is the run time rule for object casting in the above scenario? Thanks Veena
|
SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
|
 |
Sridhar Srikanthan
Ranch Hand
Joined: Jan 08, 2003
Posts: 366
|
|
Veena Does this help ? Sri
|
 |
Veena Pointi
Ranch Hand
Joined: Jun 20, 2002
Posts: 442
|
|
u have given me example of when oldtype is class & newtype is interface,but I have asked viceversa situation.I mean oltype is interface & newtype class.I want some defined rule. Thanks Veena
|
 |
Veena Pointi
Ranch Hand
Joined: Jun 20, 2002
Posts: 442
|
|
Can anybody answer to this question?I can easily solve casting questions other than the scenario I explained above.I don't know exactly what rule applies when casting from interface type to class type. Thanks Veena
|
 |
Junilu Lacar
Bartender
Joined: Feb 26, 2001
Posts: 4118
|
|
Here's what the JLS says about Casting Conversions: http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#175725 If you find that a bit too formal and confusing... Using an interface reference to refer to an object does not change that object's runtime type. Therefore, the runtime type of the object referred to by the interface reference should be assignment compatible with the object reference you are assigning it to. That is, if you have List someList = new ArrayList(); Then, ArrayList obj1 = (ArrayList) someList; // legal - the object being referenced is an ArrayList AbstractList obj2 = (AbstractList) someList // legal - the object being referenced is a subclass of AbstractList LinkedList obj3 = (LinkedList) someList // runtime error - the object referenced is an ArrayList which is not assignment compatible with LinkedList There are additional restrictions when dealing with references to final classes but I'll leave that as an exercise for you.
|
Junilu - [How to Ask Questions] [How to Answer Questions] [MiH]
|
 |
alzamabar
Ranch Hand
Joined: Jul 24, 2002
Posts: 379
|
|
I could try to guess: how could you cast an interface if the interface cannot be istantiated? Marco
|
Marco Tedone<br />SCJP1.4,SCJP5,SCBCD,SCWCD
|
 |
Veena Pointi
Ranch Hand
Joined: Jun 20, 2002
Posts: 442
|
|
Actually concept is still not clear to me. Veena
|
 |
alzamabar
Ranch Hand
Joined: Jul 24, 2002
Posts: 379
|
|
I'll try to be clever with another example. Let's assume you have: MyInterface myInt = new MyInterfaceImpl();//This creates an object MyInterfaceImpl of type MyInterface What actually happens here is that at runtime first an instance of MyInterfaceImpl is being created and then an upcasting to its interface is done. The same result could be accomplished doing the following: MyInterfaceImpl myObj = new MyInterfaceImpl(); MyInterface driver = (MyInterface)myObj; Since you cannot create an instance of an interface like: MyInterface myInt = new MyInterface(); as an interface cannot accomplish any work because its methods don't contain any implementation and therefore there is no logic in creating an instance of an interface trying something like: MyInterfaceImpl myImpl =(MyInterfaceImpl) myInt; would attempt to create an instance of MyInterface first then trying to cast it to a class; but we told before that an interface cannot be instantiated, therefore such a casting is not possible. Hope it will help, Marco
|
 |
Junilu Lacar
Bartender
Joined: Feb 26, 2001
Posts: 4118
|
|
Originally posted by Marco Tedone: ... The same result could be accomplished doing the following: MyInterfaceImpl myObj = new MyInterfaceImpl(); MyInterface driver = (MyInterface)myObj; The cast is not required. As long as myObj implements MyInterface, the following are equivalent: // 1 MyInterface driver = new MyInterfaceImpl(); // 2 MyInterfaceImpl myObj = new MyInterfaceImpl(); MyInterface driver = myObj; MyInterfaceImpl myImpl =(MyInterfaceImpl) myInt; would attempt to create an instance of MyInterface first then trying to cast it to a class; but we told before that an interface cannot be instantiated, therefore such a casting is not possible. Such a casting is in fact possible. In a way, you are right: You are not actually casting the interface, but the object behind the interface. Remember, an interface reference is just another way of "seeing" an object. You only "see" what the interface defines. The underlying object can have other capabilities. Which is why this would work: Hope this helps clarify the concepts a bit. [ February 12, 2003: Message edited by: Junilu Lacar ]
|
 |
alzamabar
Ranch Hand
Joined: Jul 24, 2002
Posts: 379
|
|
Originally posted by Junilu Lacar: Originally posted by Marco Tedone: ... The same result could be accomplished doing the following: MyInterfaceImpl myObj = new MyInterfaceImpl(); MyInterface driver = (MyInterface)myObj; The cast is not required. As long as myObj implements MyInterface, the following are equivalent: // 1 MyInterface driver = new MyInterfaceImpl(); // 2 MyInterfaceImpl myObj = new MyInterfaceImpl(); MyInterface driver = myObj; MyInterfaceImpl myImpl =(MyInterfaceImpl) myInt; would attempt to create an instance of MyInterface first then trying to cast it to a class; but we told before that an interface cannot be instantiated, therefore such a casting is not possible. Such a casting is in fact possible. In a way, you are right: You are not actually casting the interface, but the object behind the interface. Remember, an interface reference is just another way of "seeing" an object. You only "see" what the interface defines. The underlying object can have other capabilities. Which is why this would work: Hope this helps clarify the concepts a bit. [ February 12, 2003: Message edited by: Junilu Lacar ]
|
 |
 |
|
|
subject: Question on casting
|
|
|