• 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

Overloaded method and invocation.

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

Let me quote one thing from the book:

"Reference type determines which overloaded version (based on declared argument types) is selected. Happens at compile time. The actual method that's invoked is still a virtual method invocation that happens at runtime, but the compiler will already know the singature of the method to be invoked. So at runtime, the argument match will already have been nailed down, just not the class in which the method lives".

The second part of this quotation is not very clear for me. What do they mean by virtual method? Singuare? The argument match will already have benn nailed down? Just not the class in which the method lives?

Could someone please explain me what do they mean here?
And do we speak here about the situation like this: ?



and that the method is selected only on the reference type, so if we try to invoke lala(new Animal(), new Animal()) we'll get a compile error?
but also please clarify the quotation, I'm very curious.

Cheers,
Jan.
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jan,

I think the author means virtual in the sense that the method that will be doing the actual work is not
known during compilation.

But, since the signature (i.e. the name of the method and the number, order and types of formal parameters)
is known, the compiler does know which overloaded version of the method will be chosen.

Ultimately, at runtime, it is possible to know which actual, as opposed to virtual, method will be chosen,
because only then the real class is known.

Your code snippet shows only part of what is needed to explain this, because it is easier to understand
when there is some inheritance/sub-classing involved...

Suppose a class Zoo that implements interface Habitat and let's say in Habitat you defined
two methods that look like the ones you show.

Given...



During compile you don't know which class instance will actually be returned by Zoo.getZoo(), because
Zoo might itself be extended by other classes which might override Zoo's methods.

What you and the compiler do know is that, whatever the 'extending' class will be, it will be a method having
the signature lala(Object a, Animal b) that will do the actual work...because of the choice made on the
last line of the snippet...

...I hope it is a bit clearer

Gian
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By virtual method they mean the overridden method.
Signature of method to be called at runtime is decided at compile time itself. This does not mean the actual method to be called is also decided at compile time because the method could have been overridden. (though in case of overloading the actual method can also be determined at compile time).

With the below code, you will not get any error in the call lala(new Animal(), new Animal()), first method will be called as Animal is an Object. But the code will fail to compile if you have another method like void lala(Animal b, Object a){} because now there are 2 methods which can match the call lala(new Animal(), new Animal()).
If you have yet another method void lala(Animal a, Animal b) (total 4 methods now), this method will be called and there will be no compilation error.


Hope this clarifies your doubt.
 
Gian Franco
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...from virtual method

In object-oriented programming, a virtual function or virtual method is a function
or method whose behaviour can be overridden within an inheriting class by a function
with the same signature. This concept is a very important part of the polymorphism
portion of object-oriented programming (OOP).


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic