The Artima Developer Community
Sponsored Link

Java Answers Forum
Why is it useful in downcast not upcast?

2 replies on 1 page. Most recent reply: Jul 25, 2003 11:00 AM by Greg Lehane

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Dor

Posts: 13
Nickname: eis
Registered: Jul, 2003

Why is it useful in downcast not upcast? Posted: Jul 25, 2003 5:38 AM
Reply to this message Reply
Advertisement
In the book, it says:
"
if(x instanceof Dog)
((Dog)x).bark();

The if statement checks to see if the object x belongs to the class Dog before casting x to a Dog. It’s important to use instanceof before a downcast when you don’t have other information that tells you the type of the object; otherwise, you’ll end up with a ClassCastException."


I don't understand this: concerning the 'if' sentence, if x was 'instanceof' Dog, there's no need to downcast at all; on the other hand, when x was not (which would happen, if x was some instance of the class that Dog is actually inherited from, i.e. at the time exactly downcast would be needed), then the so-called 'downcast' here wouldn't be performed.

What's the sense of this short graph of program and the following comments?

Your reply would be very much appreciated!


zenykx

Posts: 69
Nickname: zenykx
Registered: May, 2003

Re: Why is it useful in downcast not upcast? Posted: Jul 25, 2003 5:49 AM
Reply to this message Reply
You have as parameter an Object obj and in case it is an instance of class Dog you want to call one method in class Dog.
You can do this in 2 ways: obj.getClass().getName().equals().... but is difficult and the other solution is
if (obj instanceof Dog) {
Dog aDog = (Dog)obj;
aDog.aMethod();
}

If you don' perform the downcast you cannot access the method "aMethod" which belongs to Dog.

Greg Lehane

Posts: 33
Nickname: glehane
Registered: Jun, 2003

Re: Why is it useful in downcast not upcast? Posted: Jul 25, 2003 11:00 AM
Reply to this message Reply
In practice x will usually be of type Object. Since Object is the "superest" super class (all other classes are sub classes of it).

Objects of type Object are often used as placeholders for sub-classes, because they can house any class.

I think your confusion can be explained with the following statement. If you are using an "Object" class object to house your subclass, i.e.
Object x = new Dog()
then
x instanceOf Object
will return true, and
x instanceOf Dog
will also return true.

Hope this helps,

=- Greg

Flat View: This topic has 2 replies on 1 page
Topic: don't understand why checked exceptions get swallowed. Previous Topic   Next Topic Topic: MenuBar help!!!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use