• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

up cast of object

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look at the scope.

class Foo{
public void print(){
System.out.println("this is Foo");
}
}
class Fooer extends Test{
public void print(){
System.out.println("this is Fooer");
}
}
public class RunFoo{
public static void main(String [] args){
SubTest subtest = new SubTest();
Test test = subtest;
test.print();
}
}

I thought it would print "Foo" but in fact it printed "Fooer".It seemed that the Fooer is not up cast to Foo,but why?
 
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is called dynamic binding. When you assign subtest to a variable of type test it does not change the type of the actual object. When you run the print method the JVM runs the print method of the actual object.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm confused. What is SubTest ? A subclass of Test? Your above code seems to be missing something.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're confused :^)
class Fooer extendes Test not Foo!!!
NICE EXAMPLE CODE

Originally posted by Lionel Siau:
I'm confused. What is SubTest ? A subclass of Test? Your above code seems to be missing something.

 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is something that shows polymorphic functions.
The base reference 's' of class Shape contains a reference to an object of type 'Circle', this is why the Circle.draw() is called.
Dynamic binding has to do with the 'type referenced', not the reference's type!

[ February 27, 2002: Message edited by: Rajinder Yadav ]
 
guo mark
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks all first.Its ture that there are someting wrong with my code,i forget to change Test and SubTest to Foo and Fooer.But i have got the idear,so thanks to all again.
reply
    Bookmark Topic Watch Topic
  • New Topic