About Question enthuware.ocajp.i.v7.2.1011 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1011 :
"So, FileNotFoundException class is the most specific class." Why java.io.FileNotFoundException is the most specific class?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1011 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Fri Jan 31, 2014 8:28 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1011 :
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?
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?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1011 :
The first two lines of the given explanation explain exactly how it resolves:
HTH,
Paul.
For a more thorough understanding, please go through: http://docs.oracle.com/javase/specs/jls ... #jls-15.12The 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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Mon Nov 03, 2014 5:18 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1011 :
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:
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.
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
Thanks in advance for clearing this up.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1011 :
That is correct. But you should try it out.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Tue Apr 21, 2015 5:26 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1011 :
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?
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?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1011 :
Please post exact and complete code and the exact error message you are getting.
If you like our products and services, please help us by posting your review here.
-
- Posts: 62
- Joined: Fri Aug 07, 2015 2:16 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1011 :
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
{
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
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1011 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 62
- Joined: Fri Aug 07, 2015 2:16 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1011 :
thanks.
but still I donst see how the compiler cant destinguish between those..
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)
{
}
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1011 :
Section 15.12.2 of JLS explains this in detail.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Mon Dec 14, 2015 12:20 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1011 :
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.
method(java.io.FileNotFoundException s) and method(int...o) so here the ambiguity.
-
- Posts: 23
- Joined: Fri Mar 26, 2021 7:25 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1011 :
The reason is simple, but who knows the answer before the explanation shows up.
Who is online
Users browsing this forum: Bing [Bot] and 24 guests