Strange Java null behavior in Method Overloading
why the program calls
foo(String x)
instead offoo(Object x)
That is because String
class extends from Object
and hence is more specific to Object
. So, compiler decides to invoke that method. Remember, Compiler always chooses the most specific method to invoke. See Section 15.12.5 of JLS
If more than one member method is both accessible and applicable to a
method invocation, it is necessary to choose one to provide the
descriptor for the run-time method dispatch. The Java programming
language uses the rule that the most specific method is chosen.The informal intuition is that one method is more specific than
another if any invocation handled by the first method could be passed
on to the other one without a compile-time type error.
However, if you have two methods with parameter - String
, and Integer
, then you would get ambiguity
error for null
, as compiler cannot decide which one is more specific, as they are non-covariant types.
null behavior in function overloading with polymorphism
Why the second function was selected for executing and not the 1st one?
Because the method taking NewTest
is more specific than the method taking Test
, and both are accessible and applicable.
However, when you've got two subclasses of Test
, neither conversion is more specific than the other, hence the call is ambiguous.
See JLS section 15.12.2.5 for the exact rules involved:
If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.
Why this behavior in overloading
t's because In case of method Overloading
The most specific method is choosen at compile time.
As 'java.lang.String' is a more specific type than 'java.lang.Object'. In your case the method which takes 'String' as a parameter is choosen.
Clearly mentioned in DOCS
Which method will get invoked during overloading when null is passed
As explained by Rohit here,
That is because String class extends from Object and hence is more
specific to Object. So, compiler decides to invoke that method.
Remember, Compiler always chooses the most specific method to invoke.If more than one member method is both accessible and applicable to a
method invocation, it is necessary to choose one to provide the
descriptor for the run-time method dispatch. The Java programming
language uses the rule that the most specific method is chosen.The informal intuition is that one method is more specific than
another if any invocation handled by the first method could be passed
on to the other one without a compile-time type error.However, if you have two methods with parameter - String, and Integer,
then you would get ambiguity error for null, as compiler cannot decide
which one is more specific, as they are non-covariant types.As described in Section 15.12.5 of JLS
Which overload will get selected for null in Java?
The most specific method will be called - in this case
showInputDialog(Component parent, Object message)
This generally comes under the "Determine Method Signature" step of overload resolution in the spec (15.12.2), and in particular "Choosing the Most Specific Method".
Without getting into the details (which you can read just as well in the spec as here), the introduction gives a good summary:
If more than one member method is both
accessible and applicable to a method
invocation, it is necessary to choose
one to provide the descriptor for the
run-time method dispatch. The Java
programming language uses the rule
that the most specific method is
chosen.The informal intuition is that one
method is more specific than another
if any invocation handled by the first
method could be passed on to the other
one without a compile-time type error.
Multiple overloaded methods: Does null equal NullPointerException?
If there are several overloaded methods that might be called with a given parameter (null
in your case) the compiler chooses the most specific one.
See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.5
In your case methodTest(Exception e)
is more specific than methodTest(Object e)
, since Exception is a subclass of Object. And methodTest(NullPointerException e)
is even more specific.
If you replace NullPointerException with another subclass of Exception, the compiler will choose that one.
On the other hand, if you make an additional method like testMethod(IllegalArgumentException e)
the compiler will throw an error, since it doesn't know which one to choose.
Passing null to the overridden method when the difference between methods is the parameter subtype
The method with most specific parameter type is called. Thats the rule
This is from JLS section 15.12.2.5
If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
Why will null resolve to Class? overload?
This has to do with method resolution, as defined in JLS 15.12.2. This happens at compile-time, and the compiler basically picks the most specific method that's applicable. In your case, both methods are applicable (since null
can be cast to either Class<?>
or Object
), but the Class<?>
version is more specific.
The bit about null
being applicable comes from JLS 4.1:
The null reference can always be assigned or cast to any reference type (§5.2, §5.3, §5.5).
Specificity is defined in JLS 15.12.2.5. It can be a bit overwhelming, but basically works as you'd expect: Class<?>
is a more specific type than Object
(it's a subtype of Object
), and so works to make that method overload more specific.
Unexpected Polymorphism Behaviour In Java if same method overloaded and overridden
Java uses runtime virtual dispatch for calls to instance methods (that is, those that aren't static). That still applies to calls to this
; that's how, for example, an abstract class can define an abstract method createResultObject
to be implemented by subclasses and use that method in its doProcessing
method.
In your case, the object is a CustomUtility
, so calls to this.getViews(String, String)
are dispatched to CustomUtility
, which then uses super
internally.
Related Topics
What Is the Default Stack Size, Can It Grow, How Does It Work with Garbage Collection
How to Define Multiple Jbutton Actions from a Different Class
Java Regex: Repeating Capturing Groups
How to Avoid a Lot of If Else Conditions
How to Do a Soap Web Service Call from Java Class
Increasing Heap Space in Eclipse: (Java.Lang.Outofmemoryerror)
How to Pass Parameters to Anonymous Class
Why Do I Get "Non-Static Variable This Cannot Be Referenced from a Static Context"
Gson Custom Seralizer for One Variable (Of Many) in an Object Using Typeadapter
Why Are Getter and Setter Method Important in Java
Why Do == Comparisons with Integer.Valueof(String) Give Different Results for 127 and 128
Differencebetween 'Super' and 'Extends' in Java Generics
Gradle to Execute Java Class (Without Modifying Build.Gradle)