File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting problem !!!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting problem !!!!!!" Watch "Casting problem !!!!!!" New topic
Author

Casting problem !!!!!!

sunil kathuria
Greenhorn

Joined: Jun 06, 2001
Posts: 24
Hierarchy
=========

class A
!
------------
! !
class B class C --> interface If1

line 1. If1 ob1 = new C();
line 2. B ob2 = (B) ob1; //legal at compile and illegal at run-time
line 3. C ob3 = new C();
line 4. B ob4 = (B)ob3; //illegal both at compile and run-time

my question is --> at line 2 and line4 i am assigning object
of C to Object of B .as line 2 is legal at compile time why
line 4 is not legal at compile time.
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
If I understand your hiarchy-diagram correctly, B & C are two classes that both implements If1.
Since both classes implements If1, a cast from If1 to B is allowed (according to the compiler). However, since this reference contains an instance of C, which can't be converted to a B, it will fail at runtime.
As for line 4, you have a reference of type C. Since there is no direct coinnection between B & C (yes, they both implement the same interface, but references only work downwords in hiarchy, not sideways) the compiler will object that it doesn't know how to convert between the to classes.
Does this explain it?
/Mike
Andy Skalkin
Ranch Hand

Joined: Apr 05, 2001
Posts: 33
Originally posted by Mikael Jonasson:
If I understand your hiarchy-diagram correctly, B & C are two classes that both implements If1.

According to diagram, I understand that only C implements If1. But, it still makes sense since we can try to cast from every interface to any class - and, of course, we're getting runtime error when it's failed.
Hope this helps!
Andy.
sunil kathuria
Greenhorn

Joined: Jun 06, 2001
Posts: 24
sorry there is problem in hierarchy diagram

only class C is implementing interface If1 not class B
pl consider this hierachy
class A
!
!-->class B
!
!
!-->class C implements If1
pl. answer according to above hierrachy
sunil

[This message has been edited by sunil kathuria (edited June 27, 2001).]
[This message has been edited by sunil kathuria (edited June 27, 2001).]
[This message has been edited by sunil kathuria (edited June 27, 2001).]
[This message has been edited by sunil kathuria (edited June 27, 2001).]
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
It is as Andy said: It is always (according to the compiler) ok to cast betwwen an interface and any class. The problem doesn't come until runtime as it is trying to do the acctual conversion and finds out it has no way of convering from C to B.
C doiesn't extend B now does it? Or am I still reading you diagram wrong?
/Mike
sunil kathuria
Greenhorn

Joined: Jun 06, 2001
Posts: 24
pl. solve my problem. till now i have not got the descriptive
answer
pl. help
sunil
Trevor Green
Ranch Hand

Joined: May 30, 2001
Posts: 44
Can you do us a favour and write your question a little clearer then.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Your heirarchy and your scenario do not match (Unless I am not reading your self-invented heirarchy diagrams correctly). Why don't you start another thread and describe the actual results for the heirarchy that you are questioning. This time describe the relationships instead of drawing heirarchies.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Gokhan Gultekin
Greenhorn

Joined: Jun 23, 2001
Posts: 27
Hi Sunil,
I've tried ur hierarchy on jdk1.2.2. With line 2 ur right. It does compile but fails at runtime. But in line 4 you don't even need to explicitly cast ob3 to class B, bcoz class C is a subclass of class B and u can assign it to a ref of its parent class w/o casting. Check out the code below:
--------------------------------------------------------------
interface If1
{
}
class A
{
String s = "Root class";
}

class B extends A
{
String s = "Second class";
}
class C extends B implements If1
{
String s = "Third class";
}
public class castingProblem
{
public static void main(String args[])
{
If1 ob1 = new C();
B ob2 = (B) ob1;
C ob3 = new C();
B ob4 = (B)ob3;//Here you don't need the casting
//But it should still compile.
//Try "B ob4 = ob3" in line 4
//It should compile again
}
}
--------------------------------------------------------------
Can u tell me which jdk ur using?
Cheers,
gokhan
sunil kathuria
Greenhorn

Joined: Jun 06, 2001
Posts: 24
ok i am starting new thread--casting problem(new)!!
pl. look.
sunil
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting problem !!!!!!
 
Similar Threads
casting problem(new) !!!
GC
Final Class Problem
HashMap prob
hashmap doubt