• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Overloaded methods

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Base{
public void amethod(Base b){
System.out.println("Base method");
}
}
class Over extends Base{
public static void main(String args[]){
Over o = new Over();
Base b = new Base();
Base b1 = new Over();
int iBase=0;
o.amethod(b); //1
b.amethod(o); //2
b1.amethod(b); //3
b1.amethod(o); //4
o.amethod(o); //5
}
public void amethod(Over o){
System.out.println("over method");
}
}
when compiled and run, it prints:
Base method
Base method
Base method
Base method
over method

why does line 4 print Base method?? should it not access amethod of Over class??
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
b1.amethod(o); //4
1. Since the declared type of the reference b1 is Base, at compile-time, the class Base is searched for applicable and accessible methods.
There is only one method in Base, amethod(Base b). It is applicable because the argument o of type Over can be assigned to the parameter b of type Base.
2. Here is the way the JLS (15.12.1) describes it:

In all other cases, the qualified name has the form FieldName . Identifier; then the name of the method is the Identifier and the class or interface to search is the declared type of the field named by the FieldName.



3. Here is the way The Java Programming Language (6.9.1) describes it:

The method resolution process takes place at compile time based on the declared types of the object reference and the argument values. This process determines which form of a method should be invoked, but not which implementation of that method. At run time the actual type of the object the method is invoked upon is used to find an implementation of the method that was determined at compile time.


[ March 11, 2003: Message edited by: Marlene Miller ]
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose we add a method hello() to superclass Base and Subclass Over that gets overriden in Over; and also lines //6,//7,//8:

Results in:
in Base //6
in Over //7
in Over //8
Why does line 8 print 'in Over' ( i.e.accessing class Over's implementation of hello() )if b1's declared type is Base?
Fabricio
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should help you. Overriding/Overloading
reshma
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does line 8 print 'in Over' ( i.e.accessing class Over's implementation of hello() )if b1's declared type is Base?
Base b1 = new Over();
b1.hello(); // 8
1. The compile-time steps of method resolution (step 1. determine the class to search, step 2a. select the methods that are applicable and accessible, step 2b. select the most specific method) help us to figure out which form (think overloading) of the method will be invoked.
The run-time steps of method resolution help us to figure out which implementation (think overriding) of the method will be invoked.
2. Since the declared type of the reference b1 is Base, at compile-time, the class Base is searched for applicable and accessible methods.
There is only one method in Base, hello(). It is applicable because there are no arguments in the method invocation b1.hello() and there are no arguments in the Base.hello().
At run time, the target reference is computed. The target reference is the value of b1, that is, a reference to an object of type Over. Since the declaration in Over overrides the declaration in Base, Over.hello is invoked.
[ March 11, 2003: Message edited by: Marlene Miller ]
[ March 11, 2003: Message edited by: Marlene Miller ]
 
Fabricio Sanchez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Reshma and Marlene, both the link and the explanation helped a lot + made it really clear.
I didn't realise that overloaded methods and overriden methods where looked at by the compiler and during run-time in different ways.
You are both stars!
Fabricio
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic