• Post Reply Bookmark Topic Watch Topic
  • New Topic

Following code shows ClassCastException  RSS feed

 
Minal Silimkar-Urankar
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Line below gives runtime error ClassCastException, it does not give any error or warning at compile time
B b=(B)new A();
 
Meet Gaurav
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you cannot do this. Actually wat you are trying to do here ?

A is a Super class of B

You are trying something like this
Exception aa = new Throwable(); \\ Impossible in Java
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Down casting is not allowed in Java.
Like,



You cant assign parent object to child reference, you cant put big cup size sugar materiel into a small cup.

Now,
You can compile this program, by telling the compiler, like..



Now, when you call.



As the down-cast is invalid since an object created as type A doesn't necessarily support all the operations type B does. Thus throws exception..
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me give a simple example to show what Sagar is saying.



Now if you do this

Dog dog = (Dog)new Animal();

If this doesn't throw exception, then you will try this

dog.bark();

But dog reference actually points to an Animal. And an animal cannot bark. So the Cast itself will fail...
 
Casttro Francis
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Meet Gaurav is exactly rite. Downcasting is not possible in Java..

You are trying something like this
Exception aa = new Throwable(); \\ Impossible in Java

Throwable is a super class of Exception.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just adding one brick to the wall




Because it is run time decision/assignment what dog reference will refer to?
Compiler needs to keep silent until this assignment happens in real(at runtime).

[Corrected:] Changed up casting to down casting in comment
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandra Bhatt wrote:
You mean downcasting, surely?
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandra,I guess you mean DOWN CASTING here.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One More thing Polymorphism and Inheritance is always tricky. So Practice, Practice and More Practice...
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I meant down casting.

Thanks for correction.

 
Minal Silimkar-Urankar
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Chandra Bhatt wrote:



As you shown in your code, using sub class reference if I try to refer to super class using casting it will work. But code I tried gives class cast exception.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
minal silimkar wrote:

Chandra Bhatt wrote:



As you shown in your code, using sub class reference if I try to refer to super class using casting it will work. But code I tried gives class cast exception.


Using sub class reference that actually points to super class object, you can only call methods that have been overridden by the subclass.
In other words methods specific to sub class can not be invoked in this way.
In fact the dog reference points to Animal object at run time, if Animal class doesn't have makeSound(), it fails.
 
Minal Silimkar-Urankar
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chandra Bhatt

Using sub class reference that actually points to super class object, you can only call methods that have been overridden by the subclass. In other words methods specific to sub class can not be invoked in this way.


If you check code I originally posted, I am invoking overriden method in main, which gives runtime error ClassCastException.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is going to fail if the sub class reference refers to super class object at run time, giving you ClassCastException.
Compiler forgives us doing down casting, thinking the reference variable may refer to sub class object.

 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
minal silimkar wrote:


Line below gives runtime error ClassCastException, it does not give any error or warning at compile time
B b=(B)new A();

Minal,
If we simplify your main method's content,
In line 1 we have created a reference 'a' of type A which refers to Object of type A. In line 2 we have created a reference 'b' and we are trying to DOWNCAST the reference of type A to type B. As A and B are in the same hierarchy compiler won't complain about DOWNCASTING. Now the current situation is a reference b of type B refers to object of type A. And compiler comes to know about this fact at runtime because at compile time it is still thinking that it would point to correct object type (optimistic ) . but when it comes to know about the fact that it is pointing to wrong object type it fails (crashes with broken heart). Now tell me , will below work?What would be the output?
 
Minal Silimkar-Urankar
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it gives "Run Time Error"

Thanks Vishal for detailed explanation
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!