Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1011 :

Posted: Thu Dec 20, 2012 7:22 am
by ETS User
"So, FileNotFoundException class is the most specific class." Why java.io.FileNotFoundException is the most specific class?

Re: About Question enthuware.ocajp.i.v7.2.1011 :

Posted: Thu Dec 20, 2012 8:33 am
by admin
As you go from superclass to subclass, you get more specific. Since FileNotFoundException class is the subclass of IOException, FileNotFoundException is more specific than IOException. By the same logic IOException is more specific that Object. Therefore, out of FileNotFoundException , IOException, and Object, FileNotFoundException is the most specific class.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1011 :

Posted: Thu Dec 20, 2012 9:23 am
by Guest
Thanks i understood.

Re: About Question enthuware.ocajp.i.v7.2.1011 :

Posted: Sat Mar 15, 2014 12:28 pm
by srao.nagaraj
Sorry, I need some clarification here.

tc.method(null).

Here null does not represent any object . How will the compiler resolve it to any of these overloaded methods. Could you please help elaborate?

Re: About Question enthuware.ocajp.i.v7.2.1011 :

Posted: Sat Mar 15, 2014 9:28 pm
by admin
The first two lines of the given explanation explain exactly how it resolves:
The reason is quite simple, the most specific method depending upon the argument is called. Here, null can be passed to all the 3 methods but ///FileNotFoundException/// class is the subclass of ///IOException/// which in turn is the subclass of ///Object///. So, ///FileNotFoundException/// class is the most specific class.
For a more thorough understanding, please go through: http://docs.oracle.com/javase/specs/jls ... #jls-15.12

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1011 :

Posted: Tue Nov 04, 2014 6:07 am
by Kevin_C
Never knew this.. So some things to make sure I understand it correctly.

With methods that take Objects as parameters when multiple are possible: it either takes the lowest subclass when they are all of the same tree-branch (like in this question) or gives a compile time error when they aren't from the same tree-branch (like with String and StringBuffer of the example).

With methods that take primitives as parameters when multiple are possible, it first looks at least number of modifications / exact matches, then oldest java version (first widening, then boxing/unboxing). For example:

Code: Select all

void method(int... x){ ... } //1
void method(Integer x){ ... } //2
void method(long x){ ... } //3
void method(Object x){ ... } //4
When method(4) is called, it will use //3 [widening is preferred over boxing]. When method(new Integer(4)) is called, it will use the //2 [exact match]. When method(new Long(4)) is called, it will use //4 [superclass is preferred over boxing/unboxing]. Are these all correct? (I'm not really sure about the last one.)

Thanks in advance for clearing this up.

Re: About Question enthuware.ocajp.i.v7.2.1011 :

Posted: Tue Nov 04, 2014 12:58 pm
by admin
That is correct. But you should try it out.
HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1011 :

Posted: Thu Apr 23, 2015 5:00 am
by girish_v
Surprising. If i add another method
public void method(int...o){
System.out.println("Object Version");
}

this throws compile error. Why? In fact, only having method(int..o){} and method(java.io.IOException s){}, throws ambiguity. Why is this?

Re: About Question enthuware.ocajp.i.v7.2.1011 :

Posted: Thu Apr 23, 2015 10:06 am
by admin
Please post exact and complete code and the exact error message you are getting.

Re: About Question enthuware.ocajp.i.v7.2.1011 :

Posted: Tue Aug 11, 2015 4:54 pm
by sir_Anduin@yahoo.de
public class Main
{
public static void main(String args[]){
Main tc = new Main();
tc.method(null);
}

public void method(int...o){
System.out.println("Array version");
}



public void method(Object o)
{
System.out.println("Object Version");
}

public void method(java.io.FileNotFoundException s)
{
System.out.println("java.io.FileNotFoundException Version");
}

public void method(java.io.IOException s)
{
System.out.println("IOException Version");
}
}

This is the code girish_v probably used, and I get:
Error:(11, 11) java: reference to method is ambiguous
both method method(int...) in ExamExamples.Main and method method(java.io.FileNotFoundException) in ExamExamples.Main match

Re: About Question enthuware.ocajp.i.v7.2.1011 :

Posted: Tue Aug 11, 2015 7:45 pm
by admin
OK, the error message is quite clear. There multiple methods that are equally applicable for the method(null) call. So the compiler is confused and refuses to compile.

Re: About Question enthuware.ocajp.i.v7.2.1011 :

Posted: Wed Aug 12, 2015 1:40 am
by sir_Anduin@yahoo.de
thanks.

but still I donst see how the compiler cant destinguish between those..

Code: Select all

public void method(int...o){
}
public void method(Object o)
{
}

Re: About Question enthuware.ocajp.i.v7.2.1011 :

Posted: Wed Aug 12, 2015 2:52 am
by admin
Section 15.12.2 of JLS explains this in detail.

Re: About Question enthuware.ocajp.i.v7.2.1011 :

Posted: Wed Dec 16, 2015 4:21 am
by santhoshi
Here other than super classes version Object and IoException,the specific one is FileNotFoundException and the array,here null can be passed to both methods

method(java.io.FileNotFoundException s) and method(int...o) so here the ambiguity.

Re: About Question enthuware.ocajp.i.v7.2.1011 :

Posted: Sat Jun 05, 2021 1:01 am
by baichen7788
The reason is simple, but who knows the answer before the explanation shows up.