• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

late binding

 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I' m not very good with late binding.... I can't know exactly what method will be chosen by the virtual machine!!!
does late binding applies only to instance methods??? how about instance variables, instance methods or class variables???
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Leandro,
Late binding only applies to instance methods. Variables (static or not) are bind at compile time based on the type of the reference used (see last two lines of output).
Briefly, if class B extends class A and overrides method m of class A then at runtime
for "A a = new B(); a.m();" method m of class B will be called.
see the code below:

[ February 04, 2003: Message edited by: Dan Culache ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Call binding from Thinking in Java
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Dan,
I wanted to ask you about what the print statements regarding C do in the main method.
I get what C.m() does, calls the m method of B.
But what do the last two print statements do?
Thank you
Pallavi
 
Dan Culache
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pallavi,
The last two print statements are printing the variables of the object refered by C (one static, one instance). I was trying to show that binding the variables is resolved at compile time as oposed to method binding which is done at runtime.
Therefore because C is a reference to a LtA object (although it points to a LtB object) the static int is of the LtA class and the int i of a LtA instance are printed.
When method m() is called, the visible m() method of the refered object is called and because LtB had overriden m() when extending LtA and because C, although a LtA reference points to a LtB object then the only visible m() method is m() of LtB.
See the code below. It prints the variable corresponding to a LtB instance, the instance C really refers to. That is if you comment line 2. Here although C points to a LtB object the compiler doesn't know who k is because C is a reference to a LtA object and LtA has no k.
A bit too long of an explanation but I hope I was clear.

[ February 04, 2003: Message edited by: Dan Culache ]
 
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic