This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem with reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem with reference" Watch "Problem with reference" New topic

Problem with reference

Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Hi in below listed problem we are getting compile time error. If we replace the StringBuffer with Object, it works fine and prints "String Version". Can pl somebody throw some light on this?

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();
Amond Adams
Ranch Hand

Joined: Nov 28, 2000
Posts: 62
Interesting question indeed.
Now the mechanism of overloading narrates that types are compared and the exact match is given the torch(parameter(s)) to run. In case of no exact match the relative to the parameter (if legally and implicitly possible) is choosen. Yet null is nothing no Type. Reasoning should say that either a Runtime exception be thrown or atleast Object version to be picked up as that is the most generic type. Yet String is picked without a hitch. I bet that in the case of an ambigious type match, ONLY when super and sub are involved, sub is chosen instead of super because sub also is a reflection of super in addition to itself.
As in the case of javax.swing.JDialog(Frame frame) and javax.swing.JDialog(Dialog dialog) Please note that Frame and Dialog are siblings they are not related except they hail from the same parent Window. In this case as stated above Compiler will straight away issue a ambigious error. Hence my point that sub is selected because it contains Super and itself at the same time thus no ambigiouity.

Two things before making any conclusions:
1. null is assignable to anything, except primitives.
2. When a single method(AnyObject ao) is called such as method(null), that method is called, that does not mean that null was promoted to type AnyObject, just that it was legally assigned the null.
3. In the case of overloaded methods in parent sub configuration when you try invoking method(null) the null is neither molded into parent type nor sub, as that is not done in any case, it is assigned to the sub type because sub is also the type of parent while parent is not the type of sub. Runtime arbitrarily invokes sub not the parent due to the already stated reasons.
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Thanks Amond. What is said is correct. When there is a overloaded methos with takes the surer class type and another derived class type, JVM whill choose the derived class type. there is one example.
It prints "Integer" because Integer is subclass of Number.
public class Test7 extends Thread{
public static void main(String args[]) {

Test7 t = new Test7();

void dis(Number a) {


void dis(Integer a) {


Ranch Hand

Joined: Oct 31, 2000
Posts: 54
Dear Amond
Hi !!
Could u pls throw some more light over similarities between String and StringBuffer class and super and sub concept. Is String a sub class of StringBuffer class ??
Thanx in advance.
Consider Paul's rocket mass heater.
subject: Problem with reference
Similar Threads
Function overloading
overload question
About String & StringBuffer
Regarding String