Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Question On overloading?

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Can any body help me out for this Question?

package example;


public class AB {

public void printAB(Object o){
System.out.println("Object");
}

public void printAB(AB a){

System.out.println("A");
}
public void printAB(BA b){
System.out.println("B");
}

public static void main(String args[]){
AB a = new AB();
a.printAB(null);
}
}
class BA extends AB{
}

if i run the program output :B

may i know the reason?
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm not sure about compile it cos i lazy to ...

but i also wonder why the output is B as you described(i don't compile it)...
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when we overload a method and try calling it then the method call would try to match the most nearest method's parameter.
since BA is subclass of AB so it prints B.Had it not been there it would have printed A.
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

this has been discussed a number of times in this forum. One of the search results - Maximally specific method

Soumya.
reply
    Bookmark Topic Watch Topic
  • New Topic