Two Laptop Bag
The moose likes Beginning Java and the fly likes about upcasting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about upcasting" Watch "about upcasting" New topic

about upcasting

lang lang

Joined: Nov 01, 2003
Posts: 8
class Instrument{
public void play(){}
static void tune(Intrument i){
class Wind extends Instrument[
public static void main(String[] args){
Wind flute=new Wind();
I am confused about 'upcasting',would you say something about it ?
how to use it?
thank you !
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

upcasting is when you refer to an object by its more general identity.
In your example you are passing flute which is a Wind object into the
tune() method which take Instrument object. So within the Instrument.tune()
your flute is seen as Instrument object, Wind object.
Just like when your neighbour just bought a new Subaru Outback, and your
wife told you, "oh, Jim just bought a new Car".
Gorkhali Mountainman

Joined: Nov 03, 2003
Posts: 1
*Here is more Simple example! Car is parent Class and Hammer is child *class! upcasting means calling method or variable from upper class!
class Car {
public static void drive(){
System.out.println("I drive Car");
class Hammer extends Car {
public static void drive(){
System.out.println("I drive Hammer!");
public static void main(String[] args){
Hammer H2 = new Hammer();
//call drive method from parent class Car;//upcasting
//call drive method from child class Hammer;
Have you checked out Aspose?
subject: about upcasting
It's not a secret anymore!