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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

Post by ETS User »

"So, FileNotFoundException class is the most specific class." Why java.io.FileNotFoundException is the most specific class?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Guest

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

Post by Guest »

Thanks i understood.

srao.nagaraj
Posts: 6
Joined: Fri Jan 31, 2014 8:28 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Kevin_C
Posts: 14
Joined: Mon Nov 03, 2014 5:18 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

That is correct. But you should try it out.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

girish_v
Posts: 7
Joined: Tue Apr 21, 2015 5:26 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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.

sir_Anduin@yahoo.de
Posts: 62
Joined: Fri Aug 07, 2015 2:16 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

sir_Anduin@yahoo.de
Posts: 62
Joined: Fri Aug 07, 2015 2:16 pm
Contact:

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

Post 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)
{
}

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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.

santhoshi
Posts: 1
Joined: Mon Dec 14, 2015 12:20 am
Contact:

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

Post 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.

baichen7788
Posts: 23
Joined: Fri Mar 26, 2021 7:25 am
Contact:

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

Post by baichen7788 »

The reason is simple, but who knows the answer before the explanation shows up.

Post Reply

Who is online

Users browsing this forum: No registered users and 23 guests