Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Following code shows ClassCastException

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
Ranch Hand
Posts: 492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 75637
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chandra Bhatt wrote:

You mean downcasting, surely?
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So it gives "Run Time Error"

Thanks Vishal for detailed explanation
 
Stop it! You're embarassing me! And you are embarrassing this tiny ad!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic