Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about overloading

 
wei liu
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why are there two different running results for the two piece of code?
public class AQuestion
{
public void method(Object o)
{
System.out.println("Object Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
Answers
The code does not compile.
The code compiles cleanly and shows "Object Version".
The code compiles cleanly and shows "String Version"
The code throws an Exception at Runtime.
the ans is 3rd.
i thought if the code were written like 'questin.method("null")', the ans were 3rd, but .... i dont know why?
----------
Question 124)
Read the code below. Will be the result of attempting to compile and run the code below.

public class AQuestion
{
public void method(StringBuffer sb)
{
System.out.println("StringBuffer Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
Answers
The code does not compile.
The code compiles cleanly and shows "StringBuffer Version".
The code compiles cleanly and shows "String Version"
The code throws an Exception at Runtime.
the ans is first one. why? only because of "public void method(StringBuffer sb)?"
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi wei,
i didnt know this earlier but i concluded from ur example that,
if there is method ambiguity like u have here, then always method with child class will be invoked.
in first example, String is child of Object. so method with String is invoked.
in second example, String & StringBuffer don't have parent, child relationship. so compiler complains.
try this,

it will give output "b1". but if u remove "extends a" and make class b not extending a class then it gives compilation error.
hth,
maulin.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry,
i forgot to pass null argument in main method to method m(). it should be,
t.m(null);
regards
maulin.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops!
m so sorry. it should be method "f()" not "m()". really sorry for so many mistakes.
maulin.
 
wei liu
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Maulin!
in my code , there is no class relationship such as a & b which appear in your code.well, i m still confused about it.is there anyone who has other explanations?
 
chen qing
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class Object is the root of the class hierarchy. Every class,including String,has Object as a superclass.
 
Suraj Berwal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by chen qing:
Class Object is the root of the class hierarchy. Every class,including String,has Object as a superclass.

adding to the above...
String Buffer and string are not having subclass relationship. So when u try to have two methods having the above as parameters(String/StringBuffer)the compiler has problems to find out which method it should refer to at compile time as both accept "null"as arguments.
In the case of "Object/String" as parameters to methods the compiler does the overriding of method with "object" as parameter by the method having "String" as parameter.

------------------
Suraj Berwal
Sun Certified Programmer for Java 2 Platform
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
before we go into the details....
what does the parameter null mean ? is it referring to an object called as null, which is basically referring to none. so what exactly does a null here stand for. that is the crux here. once we know this then all would be clear.
atleast i have not been able to exactly figure out what does null when passed as parameter mean ?

Originally posted by Maulin, Vasavada:
oops!
m so sorry. it should be method "f()" not "m()". really sorry for so many mistakes.
maulin.

 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i thought u would b able to perceive relation between Object and String class as String is child of Object
next time i will try to b more clear.
regards
maulin.
Originally posted by wei liu:
thanks Maulin!
in my code , there is no class relationship such as a & b which appear in your code.well, i m still confused about it.is there anyone who has other explanations?

 
serendipity
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a test as following:
class a {}
class b extends a {}
public class Test {
void f(a a1) {
System.out.println("a1");
}
void f(b b1) {
System.out.println("b1");
}
static void main(String[] s) {
Test t = new Test();
t.f(null);
}
}
This is the previous example and the result is : b1
I modify this example like the following:
class a {}
class b extends a {}
class c {}
public class Test {
void f(a a1) {
System.out.println("a1");
}
void f(b b1) {
System.out.println("b1");
}
void f(c c1) {
System.out.println("c1");
}
static void main(String[] s) {
Test t = new Test();
t.f(null);
}
}
And when run this example, compiler error generated:
"reference to f is ambiguous, both method f(b) in Test and method f(c) in Test match."
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i guess maulin had made it clear.
let me give a try......
now in your example you have provided 3 overloaded methods. and two the methods can be traced down to their classes which are in a heirarchy.
but the class C is not. ok.
now we know that the more specific amongst the classes is chosen clearly out of the 3 now we are left with C and B. (amongst A and B, B is more specific)
and your compiler is complaining that between B and C it is finding pretty ambigous which one to choose. the compiler is finding it hard to choose the more specific amongst B and C.
got it.... actually i spent half the day on this problem. but anyway it was worth it to know this.
specific means the class more and more down the heirarchy. like class b extends a
b is more specific here.
Originally posted by serendipity:
I have a test as following:
class a {}
class b extends a {}
public class Test {
void f(a a1) {
System.out.println("a1");
}
void f(b b1) {
System.out.println("b1");
}
static void main(String[] s) {
Test t = new Test();
t.f(null);
}
}
This is the previous example and the result is : b1
I modify this example like the following:
class a {}
class b extends a {}
class c {}
public class Test {
void f(a a1) {
System.out.println("a1");
}
void f(b b1) {
System.out.println("b1");
}
void f(c c1) {
System.out.println("c1");
}
static void main(String[] s) {
Test t = new Test();
t.f(null);
}
}
And when run this example, compiler error generated:
"reference to f is ambiguous, both method f(b) in Test and method f(c) in Test match."

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic