aspose file tools*
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about upcasting" Watch "about upcasting" New topic
Author

about upcasting

lang lang
Greenhorn

Joined: Nov 01, 2003
Posts: 8
class Instrument{
public void play(){}
static void tune(Intrument i){
//...
i.play();
}
}
class Wind extends Instrument[
public static void main(String[] args){
Wind flute=new Wind();
Intrument.tune(flute);//upcasting
}
}///:-
I am confused about 'upcasting',would you say something about it ?
how to use it?
thank you !
----Lang
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
Greenhorn

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!
*-@@@
******/
//Hammer.java
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
Car.drive();//upcasting
//call drive method from child class Hammer
H2.drive();
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about upcasting