• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

OverLoading

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class MyClass
{

public meth1(String str)
{
System.out.println("meth1: String");
}
public meth1(Object obj)
{
System.out.println("meth1: Object");
}

public static void main(String args[])
{
MyClass a =new MyClass();
a.meth1("abc");
}
}

The out put is meth1: String
But why didn't print meth1: Object
as even String is of type Object.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that is what you expect, what would it take, in your view, for the first method to be invoked?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because Java takes the most specific method it can find in cases like this. Since you are calling the method with a String as parameter, it obviously calls the version that takes a String as a parameter.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!