• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

class cating

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it not possible to downcast a class???
Eg:-
class Animal{
//some code
}
class Dog extends Animal{
//some code
}
public class Test{
public static void main(String[] args)
{
Animal a=new Animal();
Dog d=(Dog)a;
}}
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Animal a=new Dog();
Dog d=(Dog)a;

the above represents a downcasting.And the code you have supplied will give classcastexception.This is clearly explained in page no-113(k&b).
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You sure can downcast from a parent type to a child type. Now, whether the casting operation will succed will depend on the runtime type of the object being cast.

So in your example, the code will compile but you'll get a ClassCastException at runtime.

Try the following code sample



You may want to read about casting from the JLS
http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.5
http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#25379

HTH
Ashish Hareet
[ September 04, 2008: Message edited by: Ashish Hareet ]
 
chennuru viswanath
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the reply....
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the lines above, you have an Animal object, and by casting it to a Dog object, you tell the compiler: "Look, I have this object here, and you should treat it as an instance of class Dog".

This can only work if the object really is a Dog object. If the object you're casting isn't a Dog object, you'll get a ClassCastException when you run the code. Casting does not magically convert one type of object to a different type of object.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can we check whether the object is of type dog and then downcast using instance of
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rajeev Ja:
can we check whether the object is of type dog and then downcast using instance of



Absolutely, in the code sample from the original post you can do the following



which will prevent the ClassCastException from being thrown.

HTH
Ashish Hareet
[ September 04, 2008: Message edited by: Ashish Hareet ]
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here you can refer the Dog object to the Animal reference and using instanceof, at runtime check whether the instance is actually of type Dog.
instanceof is typically used in polymorphism, where at runtime the actual type of object is checked.
If you do:

The if condition would always be false since an Animal object dosen't satisfy the IS-A condition for Dog.
[ September 04, 2008: Message edited by: Somnath Paul ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic