-12

Scenario 1: What if we pass null in constructor of a class and we have an overloaded constructor taking Object and second constructor having String as argument

Scenario 2 : What if we pass null in constructor of a class and we have an overloaded constructor taking Integer and second constructor having String as argument and third constructor has Object as argument

Scenario 3 : What if we pass null in constructor of a class and we have overloaded constructor taking Exception and second constructor having ArithmeticException as argument and third constructor has Object as argument

Answer to case 1 String

Answer to case 2 Compiler error , Ambiguous methods

Answer to case 3 ArithmeticException

Please explain me the logic behind the above answers

awksp
  • 11,764
  • 4
  • 37
  • 44
Noopur
  • 7
  • 1
  • 4
  • You're basically retyping the question of your previous post, without the code. And they're pretty self-explanatory. – AntonH May 03 '14 at 22:45
  • How it is self explanatory ? There is nothing mention like that in Oracle docs – Noopur May 03 '14 at 22:48
  • I don't think the answer to scenario 1 is String. Actually it's ambiguous as String and Integer are in the same "level" in the object hierarchy. – Alexis C. May 03 '14 at 22:49
  • I ran the code and answers written in the site are correct – Noopur May 03 '14 at 22:50
  • Follow the link I included. I googled it and found it. Try a little harder next time. – awksp May 03 '14 at 22:50
  • @Noopur Scenario 1 in action for the proof... http://ideone.com/LLqsXo – Alexis C. May 03 '14 at 22:53
  • @ZouZou I think he mistyped the first scenario somehow. The blog he linked last time has `String` and `Object` – awksp May 03 '14 at 22:54
  • my mind is not working as it is already 4am , but i will check this post , thanks for the link Method overloading for null , good night – Noopur May 03 '14 at 22:55
  • Noopur, if you go to the second page of the questions that you linked to in your earlier question, there's a link down the bottom to a page that gives full explanations of all the answers. Please don't ask the SO community to retype information that you already have. – Dawood ibn Kareem May 04 '14 at 07:50

1 Answers1

-1

remeber it works in this way.

first searches for any ambuguity method if so compile time error.

example define A(String) and A(Integer) then calling A(null) becomes ambiguity.

so calling time compile time error.

if no ambiguity,then searches for more specific type first then least specific type.

ie if class A extends B

then B will be given first priority and then A is given importance.

so in your scenarios

case 1:

one int and one String method so no ambiguity and string one is called. however if one is integer instead of int then calling with null gives error.

case 2:ambiguity because calling method wit null can not be choosen between Integer and String as both are not hierarchical classes.

case 3: no ambiguity because all are hierarchical classes and most specific one is Arithmetic exception class so that one is choosen.

Object->Exception->ArithmeticExcepton so Arithmetic exception is choosen

Karibasappa G C
  • 2,686
  • 1
  • 18
  • 27