The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2000



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.



Posted by Alex on April 20, 2001 at 10:26 PM

Here's the advantage of upcasting:
say you have a class MusicalInstrument, and as one of it's methods there's a method caled
static MusicalInstrument tuneInstrument(MusicalInstrument m)

now, to create a flute you would extend MusicalInstrument, and perhaps would give it some more attributes, but in order to tune this flute you wouldn't have to write a new method tuneFlute(), but would rather say:

Flute f = MusicalInstrument.tune(new flute);

this would create a new object flute and when tuning it it will upcast it to the object MusicalInstrument.

Hope this helps.

> To Beneficient Man : Who answers me

> Name: Muneeb Mukhtar
> Age: 24
> Sex: Male

> I am student of MCS(Masters in Computer Sciences) in AL-Khair University of Pakistan.I have completed half of MCS and now i am studing JAVA.

> Only one concept of "UP CASTING" is teasing me too much and now i am fed up with that as all other concepts for example inheritance,interface is relating to "UP CASTING".

> My main Problem is that I have understood how "up casting" is done but i could not understood what is the "ADVANTAGE" or purpose of "up casting".

> I would be thankful to you to the crazy extent if You reply me as soon as possible.
> please mail me at


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us