Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Is Downcasting possible in Java??

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Is Downcasting possible in Java??

    Hi,

    Is downcasting possible in Java?? Googled for "Downcasting", everyone says it fails at run time.. So is there a way to achieve downcasting??


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: Is Downcasting possible in Java??

    Yes you can downcast but you may get a ClassCastException at runtime if the Object is not of the correct subtype. To avoid this you can use the instanceof operator.
    if(obj instanceof Child) {
        perform cast
    }

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Is Downcasting possible in Java??

    Agree with Junky. One more thing, downcasting is only allowed if it will be succeeded at compile time. If it can't even succeded at compile time, it definitely means that you are going to downcast an invalid type to a real invalid other type. So, if you really need to downcast an object there are two choices.
    1. Either use instanceof operator that will check if it is of correct datatype but that really doesn't matter as both results the same other than the error.
    2. The second one is to write conversion constructor for your classes for which you want to downcast....
    Also downcasting results in truncation of data most of the times so it's not preferred everytime.

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Is Downcasting possible in Java??

    Hi,

    I have 2 classes Animal and Cat. And Cat extends Animal class.
    Animal aa = new Animal();
    Cat cc = new Cat();

    //Downcasting code
    if(aa instanceof Cat)
    {
    Cat c1 = (Cat)aa;
    }

    Though i have used instanceOf operator to avoid classCast Exception, my aa object will never be an instanceof Cat obj, so Cat c1 = (Cat)aa code is never reached.
    Does this mean that downcasting is not possible in java??

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,612
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Is Downcasting possible in Java??

    It depends on your definition of "downcasting".

    You cannot convert an Animal into a Cat. But if an instance of Cat is stored as an Animal (or Object, whatever), then you can indeed cast that as a Cat. As long as the Object in question is actually a Cat, and the variable it is being stored in is a parent of Cat, then you're good. For example, you can't convert a List into a Cat (unless, for some reason, Cat extended List).

    Based on the wikipedia article for downcasting, my guess is that you're using the term incorrectly.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Is Downcasting possible in Java??

    So, i give you an example what this type of type-casting will effect the data and that's why it's prohibited.
    Animal class will have attributes say, name, gender, color, cast,origin, type etc.
    Cat class will have attributes say, gender, name, color, cast
    Now when you will try to type-cast cat object to animal, this can be done and no loss of data but on the contrary, when you type cast animal to cat, you will certainly loose some or many of your data.
    That's why this type of downcasting is prohibited but somehow you can do it as KevinWorkman has said.
    Also, in my earlier post, i told you to use conversion operator. You can write your own conversion operator to type cast from animal to cat, in the way you want to handle it.

  7. #7
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Is Downcasting possible in Java??

    @KevinWorkman , Can u explain with example what is ur understanding of downcasting. Thanks in advance

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,612
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Is Downcasting possible in Java??

    Quote Originally Posted by tcstcs View Post
    @KevinWorkman , Can u explain with example what is ur understanding of downcasting. Thanks in advance
    To use your example:

    Animal a = new Animal();
    Animal c = new Cat();
    Cat cat = (cat)c;
    c.meow(); //can't do this, there is no guarantee that c has a meow method
    cat.meow(); //can do this, the compiler knows about the meow method in Cat

    So you can store a Cat as an Animal, and you can "downcast" it to Cat when appropriate (but doing this might be a sign of a bad design).
    The compiler will let you attempt to cast the variable "a" to a Cat, but you'll get a runtime error because the instance isn't actually a Cat.

    And to use a real life example, say you have an ArrayList that isn't using generics:

    ArrayList list = new ArrayList();
    list.add(new Cat());

    The list.get() method returns an Object (since everything is an Object and anything can be stored in an ArrayList). But that wouldn't be very useful if we couldn't "downcast" the Object to its actual type:

    Cat c = (Cat)list.get(0);
    c.meow();

    Does that help?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. The Following User Says Thank You to KevinWorkman For This Useful Post:

    tcstcs (April 7th, 2011)

  10. #9
    Member
    Join Date
    Mar 2011
    Posts
    114
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Is Downcasting possible in Java??

    yeah... that cleared my doubt of down casting.