File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Upcasting.....Its confusing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Upcasting.....Its confusing" Watch "Upcasting.....Its confusing" New topic

Upcasting.....Its confusing

Kajol Shroff
Ranch Hand

Joined: Dec 04, 2000
Posts: 160
This is Kajol here. I was going through TOJ and have got stuck in UPCASTING. Bruce Eckel has given the following program for upcasting :
// Inheritance & upcasting
package c07;
class Note {
private int value;
private Note(int val) { value = val; }
public static final Note
middleC = new Note(0),
cSharp = new Note(1),
cFlat = new Note(2);
} // Etc.
class Instrument {
public void play(Note n) {
// Wind objects are instruments
// because they have the same interface:
class Wind extends Instrument {
// Redefine interface method:
public void play(Note n) {
public class Music {
public static void tune(Instrument i) {
// ...;
public static void main(String[] args) {
Wind flute = new Wind();
tune(flute); // Upcasting
} ///:~
He says that "The method Music.tune( ) accepts an Instrument handle, but also anything derived from Instrument. In main( ), you can see this happening as a Wind handle is passed to tune( ), with no cast necessary. This is acceptable; the interface in
Instrument must exist in Wind, because Wind is inherited from Instrument. Upcasting from Wind to Instrument may
´┐Żnarrow´┐Ż that interface, but it cannot make it anything less than the full interface to Instrument."
All I can understand is that the method music actually accepts an Wind (DERIVED CLASS) Handle...isnt it..but as per Bruce Eckel it is accepting Instrument (BASE CLASS) handle....
Actually i am not getting the meaning of UPCASTING can somebody throw light on it....
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Well, I can try. (I am using code sections so that the ascii pictures look better!) We know up-casting refers to assigning a subclass to a superclass. Therefore if we use the following hierarchy:

We know that Wind is a subclass of Instrument and that Instrument is a superclass of Wind.
We also know that in Java every object is really just a reference object. Using that knowledge and the above hierarchy we can say the draw the following:

We can also say that since Wind extends Instrument the lines (1) through (3) take exactly the same amount of memory in each object shown above.
Now the interesting part -> we can now see how we can up-cast at will without compiler problems. The following picture shows what happens during the up-casting:

This is actually what happens in your example. Therefore anything under Instrument in the heirarchy can be passed around as an Instrument. Even if we where to extend Wind with another object. That new object could still be passed around as an instrument! Then main idea is that if we have all the components of a class contained in another (usually subclass) then we can up-cast without compiler complaints.
It should be noted however, that even though we are passing a Wind around as an Instrument the reference variable remains of type Wind. Object variables are immutable which means that once they are created as an object they will remain as that object until they are destroyed! Also Java will always know what class the object is regardless of up-casting.
Hope this helps,
[This message has been edited by Manfred Leonhardt (edited January 14, 2001).]
[This message has been edited by Manfred Leonhardt (edited January 14, 2001).]
[This message has been edited by Manfred Leonhardt (edited January 14, 2001).]
James Baud
Ranch Hand

Joined: Jan 06, 2001
Posts: 60
Hi Kajol,
I see you already have 2 posts re: UPCASTING. It took me a while to understand Bruce's Music examples even if I knew a little about music. Hopefully with this little example I can throw some light.
1st, some small concept about the code. Say you have a class called Animal, and we know that all animals can make a sound, say when they're hungry? (clue-> lets call the behaviour of making this sound a method called cry()). The animal concept is quite abstract because we don't really know what an animal object is. We know that a Dog is an animal and in fact, we can make Dog a subclass of Animal. Hey, a Cat is an animal too and it can also cry. Let's see our code.

The key concept here is that we only need to supply a single method, wantsFood() that applies to any object reference along the Animal class hierarchy and we know that the compiler will not complain. This is what UPCASTING is; a subclass reference (dog) can be assigned up the inheritance hierarchy to a superclass reference (animal).
At runtime, the cry() method that gets invoked is whatever method that is bound to the object denoted by the reference. Within method-bindinglies POLYMORPHISM (the main idea that Bruce is trying to impart here), where a superclass is able to denote objects of its own class at runtime.
~James Baud
Talk, does not cook rice. - Chinese Proverb
[This message has been edited by James Baud (edited January 15, 2001).]

<B>~James Baud</B><P>He who asks, is a fool for five minutes;<BR>but, he who does not ask, remains a fool forever. (Chinese proverb)
Kajol Shroff
Ranch Hand

Joined: Dec 04, 2000
Posts: 160
Thanks James,
that was really helpfull.....
Manfred ......i am sorry but I couldnt get exactly the meaning in ASCII would be nice if u can explain it again
I agree. Here's the link:
subject: Upcasting.....Its confusing
It's not a secret anymore!