Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes down casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "down casting" Watch "down casting" New topic
Author

down casting

sandeep Talari
Ranch Hand

Joined: Dec 24, 2007
Posts: 63
hi friends,can anybody help me,,,, where this down cast is used
when i tried and compiled it says class cast exceptionss
1.where can i use this downcasting.
2.why class cast exception comes.
class Animal
{
public void noiseMaking()
{
}
}
class Dog extends Animal
{
public void run(){}
public static void main(String [] args)
{Animal a = new Animal();
Dog d = new Dog();
Dog d2 = (Dog)a;
d2.run();
}
}
3.explain what is not neccesity to down cast ..we can call "d.run()"
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Hiya,
1)I am not sure what you mean by downcasting, can never remember if its up or down. However in your example "downcasting" is only possible between a class and its super class, i.e you can down cast a Dog to an Animal, as it maintains the "IS A" relationship, a Dog IS A Animal. However an Animal IS NOT a Dog.

2) The ClassCastException comes, becouse you are trying to cast an Animal to a Dog (A subtype of Animal) this is not possible as it breaks the above IS A relationship.

The following is however possbile:


This is possible because the Animal reference is really pointing to a Dog object, in your example the Animal reference points to an Animal

3) The reason you can call d.run() is because run is part of the Dog class, and d is a reference to a Dog class NOT an Animal class.
Inheritance works from the super (parent) class to the sub (child) class. Therefore a Dog has all of the inheritable methods of the Animal class, so anywhere in code that requries an Animal can accept a Dog.
A Dog Class can have extra methods that its super class dosnt have, therefore an Animal is not valid for casting or being used in place of a Dog.

The Sun toturials have good explanitions of this, much better then mine, and also the SCJP 1.5 exam reference/guide kathy sierra and bert bates has a very good description of this, and is actual a very good book, I have learnt a lot from it, and I ahve been developing in Java for a long time.

Thanks
Gavin
[ February 07, 2008: Message edited by: Gavin Tranter ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Gavin Tranter:
...I am not sure what you mean by downcasting, can never remember if its up or down. However in your example "downcasting" is only possible between a class and its super class, i.e you can down cast a Dog to an Animal, as it maintains the "IS A" relationship, a Dog IS A Animal. However an Animal IS NOT a Dog...

If you diagram the relationship between Animal and Dog, it would look something like...

The superclass Animal is on top, and the subclass Dog is below it. You can think of the "up" and "down" in this context.

Upcasting is always safe. If you have a Dog reference, you can safely upcast it to Animal, because it's always true that a Dog IS-AN Animal.

But downcasting is risky. Not all Animals are Dogs, and this is why an explicit cast is required to compile a downcast from Animal to Dog. You are assuring the compiler that the object referenced is, in fact, a Dog and not some other Animal. At runtime, if the object is not really a Dog, then a runtime exception will occur.

Animal a = new Dog(); //reference is Animal, but object is Dog
Dog d = (Dog)a; //explicit cast needed for downcasting


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: down casting
 
Similar Threads
Reference variable casting
Why can't we assign parent class object to child class reference?
Another examLab casting question
whats wrong with my code?
Questio about castdown and ClassCastException (Certification)