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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting Problem" Watch "Casting Problem" New topic
Author

Casting Problem

Mushraf Khan
Ranch Hand

Joined: May 04, 2011
Posts: 37
Hello Everyone. Can anyone please explain this question which says its legal both compile time and runtime.

Following is my understanding, please correct me if I am wrong. For me line 3 & 4 is confusing. Can anyone Please explain how this is legal at compile time and runtime.


Interface Min {}

Class A {}
Class B extends A {}
Class C extends B {}

A a = new A();
B b = new B();
C c = new C();
Min m = null;
b = (C) m;

1) m = (Min) b; ---> b is cast to Interface , and m's reference type is Min.So both side is Min it compiles.
2) A a2 = (A) m; ----> m (interface ) is cast to A and the either side Left-hand -side is also A , it compiles
3) B b2 = (B) a2; ----> a2 whose refernce type is A , is cast to B but Parent(A) can't be cast to a child(B) , because Parent is not Child , unless A a2= new B(),
4) Min m2 = (C) b2; ---> b2 whose reference type is B is cast to C - > same query as above - how can Parent be casted to Child
5) C c2 = (C) m2; ---> Interface is casted to C and on the left hand side its C so compiles.







Pranav Raulkar
Ranch Hand

Joined: Apr 20, 2011
Posts: 73

Hi Mushraf,

I guess you are ok with
A a = new B(); This is upcasting.
However it's also possible to do reverse of this and is called downcasting.
B b = (A) a; //Compiles but throws a class cast exception

Let me also tell you that upcasting is automatic process, whereas downcasting must be explicitly done and is perfectly legal. There were 2 posts recently that are related to the similar scenario. Check them out http://www.coderanch.com/t/539683/java/java/inheritance
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

everytime when you try to cast subclass to a superclass,you will get a ClassCastException.

Means upcasting is not allowed..


SCJP6.0,My blog Ranchers from Delhi
Pranav Raulkar
Ranch Hand

Joined: Apr 20, 2011
Posts: 73

Shanky Sohar wrote:everytime when you try to cast subclass to a superclass,you will get a ClassCastException.

Thats incorrect. The following code works fine when subclass is cast to superclass, A a1 = (A) b;
The following though will give an ClassCastException: A cannot be cast to B
Mushraf Khan
Ranch Hand

Joined: May 04, 2011
Posts: 37
Thanks a lot Pranav. It is clear. Never knew that. Please correct me if i am wrong.

From line 3 ---> B b2 = (B) a2 . It means that refernce type of a2 which is A(Parent), is casted to B (Child) and the left hand side has to be Child that is B . Means it is legal at compile time and runtime.

Now with A a = new B()
B = (A) a -----> this compiles but gives class cast exception.
Pranav Raulkar
Ranch Hand

Joined: Apr 20, 2011
Posts: 73

Mushraf, don't get confused. Line 3 wont complain at compile time but throws ClassCastException at runtime and the later won't even compile! Lets try a real world example.
This is perfectly ok as every cat is an animal. This is called upcasting and doesn't require explicit cast i.e Now, continuing from above code, below code is perfectly legal and wont give any errorsWhy? Because the animal we are trying to cast to cat is already a cat! (Animal a = c;). What we have just done is called downcasting and in downcasting the cast is explicitly required. So would fail compile time (Its not mandatory that every animal is a cat, hence cast required). But wait, what if the animal in is a dog? This wont't affect you compile time but fails miserably at runtime by throwing ClassCastException as you cannot convert Dog to a Cat!
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Oops i did mistake.........

But the basic funda is.......
ClassCastException is thrown to indicate that we are trying to cast an object to a subclass of which it is not an instance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting Problem
 
Similar Threads
Casting problem
Assigning references problem
examlab - 1 [reference variable casting]
ExamLab Question
Casting problem