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

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

Moderator: admin

Post Reply
mehtapra
Posts: 4
Joined: Sat Oct 13, 2012 2:00 pm
Contact:

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

Post by mehtapra »

In option 3 it says this statement is valid:

class CleanConnector extends PortConnector {    public CleanConnector(int port) throws IOException, FileNotFoundException, SomeOtherCheckedException {          super(port);    } }


I am not sure if throwing 'FileNotFoundException' is valid....as in another explanation in the same question it says:

'An overriding method cannot throw a superclass exception, while a constructor of a subclass cannot throw subclass exception.'

Can you please clarify on this?

Thanks

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

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

Post by admin »

mehtapra wrote:In option 3 it says this statement is valid:

class CleanConnector extends PortConnector {    public CleanConnector(int port) throws IOException, FileNotFoundException, SomeOtherCheckedException {          super(port);    } }


I am not sure if throwing 'FileNotFoundException' is valid....as in another explanation in the same question it says:

'An overriding method cannot throw a superclass exception, while a constructor of a subclass cannot throw subclass exception.'

Can you please clarify on this?

Thanks
FileNotFoundException is valid in this case because as the explanation says a subclass constructor can throw any exception but it must throw IOException (or its superclass) as well.

The statement in the explanation for other option is also valid for the situation described in that explanation. Here, is only trying to compare the rule about exceptions in case of methods with that of the rule in case of constructor and that is why it says that the subclass constructor cannot throw just the subclass exception (observe that there is no other exception in the throws class of that constructor).

But I see your point that it is causing confusion because both seem contradictory. It has now been enhanced to avoid this confusion.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

M.Komarov
Posts: 16
Joined: Fri Sep 06, 2013 1:37 am
Contact:

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

Post by M.Komarov »

It seems to me, that I don't understand something. In the section "Explanation" read as follows
As PortConnector has only one constructor, ...
Does the phrase
...other valid code.
imply that other constructors are prohibited inside this valid code? Frankly speaking I thought that other constructors are allowed :shock: .

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

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

Post by admin »

"Other valid code" basically implies that it is irrelevant to the question. So you should only assume that it will not break the code that is already given but should not assume that it contains code that will affect the answers. So in this case, you should not assume that it contains other constructors because that would affect the answers.

Questions in the real exam follow the same pattern.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

M.Komarov
Posts: 16
Joined: Fri Sep 06, 2013 1:37 am
Contact:

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

Post by M.Komarov »

Ok, it seems to me that now I get it. Thank you!

convertor
Posts: 7
Joined: Mon Jan 27, 2014 6:25 am
Contact:

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

Post by convertor »

I still don't understand.

For example, we have code which works with an instance of some class, for example SuperClass.
this SuperClass has a subclass, for example, SubClass.
Like this:
SuperClass
|
SubClass

and we are awaiting that our code which works with superclass will work with it's SubClass.

SuperClass has IOException thrown in constructor.

but if SubClass has "As described above, it can throw any exception but it must throw IOException (or its superclass) as well." superclass of IOException, for example, Exception,
our previous code where we catch IOException and met suddenly Exception, will not work.

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

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

Post by admin »

convertor wrote: ...
our previous code where we catch IOException and met suddenly Exception, will not work.
Please read my post here: viewtopic.php?f=2&t=2319&p=6892
If you like our products and services, please help us by posting your review here.

convertor
Posts: 7
Joined: Mon Jan 27, 2014 6:25 am
Contact:

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

Post by convertor »

I understand only thing - if subclass' constructor calls superclass
constructor - that's why it should throws the same or bigger exception.

but for external code which works with such classes it is stil unclear

shining_dragon
Posts: 14
Joined: Sat Mar 01, 2014 9:12 am
Contact:

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

Post by shining_dragon »

isn't it option 1 should be the answer?

"It is not possible to define CleanConnector that does not throw IOException at instantiation."

As what I understand in the option, CleanConnector must declare IOException in constructor.

Based on Explanation:
"Constructor must declare all the checked exceptions declared in the base constructor (or the super classes of the checked exceptions)."

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

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

Post by admin »

Did you notice this part of the explanation that you've quoted, "...(or the super classes of the checked exceptions)"?
If you like our products and services, please help us by posting your review here.

vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

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

Post by vchhang »

I don't believe the explanation for option#6 is entirely correct.
An overriding method cannot throw a superclass exception
I thought an overriding method in the subclass can through the same checked exception or a subset and it can throw any unchecked exception.
while a constructor of a subclass cannot throw subclass exception...


Do you mean to say 'superclass' in the the second 'subclass' in the excerpt above?

I think the code below would be more applicable to your explanation.

Code: Select all

class A{
     public A() throws IOException{ }
     void m() throws IOException{ }   
}    

class B extends A{
     //Exception is valid here, but FileNotFoundException is invalid
     public B() throws Exception{ }

      //FileNotFoundException is valid here, but Exception is invalid
     void m() throws FileNotFoundException{ } 
}

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

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

Post by admin »

No, the given explanation and the example code is correct. Please try it out.
Unchecked exceptions have no need to be declared in the throws clause at all. So the rule about subclass/superclass is irrelevant to them.
If you like our products and services, please help us by posting your review here.

chrisbarrett
Posts: 5
Joined: Sun Aug 31, 2014 5:44 pm
Contact:

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

Post by chrisbarrett »

Answer 1:
It is not possible to define CleanConnector that does not throw IOException at instantiation.
Hmm... Is it just me (could be - it's late and I'm tired), but is that a double negative? I read that as:
It is possible to define CleanConnector that does throw IOException at instantiation
So, that's what I chose.

Reading the answer, perhaps the question should be:
It is not possible to define CleanConnector that throws IOException at instantiation.
Cheers!
Chris

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

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

Post by admin »

I don't think you are interpreting it correctly. I see it as, "Is it possible to define CleanConnector that does not throw IOException at instantiation?" Yes, it is (as shown in the option explanation). So the option is incorrect.
If you like our products and services, please help us by posting your review here.

heleneshaikh
Posts: 24
Joined: Wed Sep 02, 2015 3:43 am
Contact:

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

Post by heleneshaikh »

Hi Paul,
Before I started with Enthuware, I tried the mock exams that came with an OCA book written by Boyarski & Selikoff (http://www.amazon.com/OCA-Certified-Ass ... 1118957407).
They frequently state that whenever the code needs an import, like in this exercise (IOException), it should be mentioned in the code explicitly. If that's not the case, we should answer 'does not compile' on the exam. When can we be sure that we don't have to pay attention to the imports in the code? In this example, the answer could therefore be 'does not compile'. But I just guessed that the imports weren't an issue here. But for the exam, I must be sure :)

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

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

Post by admin »

In the exam also, you will know when the import is an issue and when it is not. You will know by looking at the options and what the question is asking.
When they say, "The following is a complete code listing...:", that is when you have to consider import statements. Otherwise, most of the time they ignore import statements.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

AndaRO
Posts: 31
Joined: Wed Feb 08, 2017 5:42 pm
Contact:

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

Post by AndaRO »

This type of question is too hard.
I don't think that this type of question is for OCA exam.

I think that overloading constructor is for exam and default constructor as well.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post by OCAJO1 »

The reason that the constructor of a subclass must throw a superclass (not subclass) of the exception that superclass' constructor throws, is it a Java agreed upon convention or there is actually a logic flow behind it?

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

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

Post by admin »

OCAJO1 wrote:
Thu Dec 13, 2018 8:33 pm
The reason that the constructor of a subclass must throw a superclass (not subclass) of the exception that superclass' constructor throws, is it a Java agreed upon convention or there is actually a logic flow behind it?
Good that you asked. You will rarely find anything in rules of programming in general, and Java in particular, that is not based on logic :)
So, yes, there is quite a bit of logic behind this rule. Any good Java book will explain it. Not sure which book are you following but
10.3.2 Throwing exceptions from initializers and constructors of Hanumant's book (Page 284)
explains it nicely.

And btw, there is no restriction on a subclass' constructor regarding throwing a subclass of exception that superclass' constructor throws. Subclass's constructor is allowed to throw any exception whatsoever as long as it is declared in its throws clause along with the exception declared in the superclass' constructor.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post by OCAJO1 »

I'm sure I'm over simplifying it, but after looking the 10.3.2, is it as simple as,

The subclass's constructor has go to the superclass's constructor, so the superclass's constructor better know what is the highest checked exception the subclass' constructor is talking about. While when overriding methods, the method being overridden already knows what the highest exception it has.

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

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

Post by admin »

no, that is not what it says.

>so the superclass's constructor better know what is the highest checked exception the subclass' constructor is talking about.

why would superclass care about what the subclass is doing??
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post by OCAJO1 »

It cares because when the subclass' constructor calls the superclass' constructor (not withstanding the case in Note) the superclass' constructor has to have the same or higher class of exception compare to the subclass' constructor.

Note: Of course when the compiler inserts a default constructor for a subclass because we did not provide one with appropriate exceptions as defined in the superclass's constructor, it automatically causes an error.

As I said it, is a oversimplified way of remembering the exception ranking order in constructors and overridden methods.

Is there a more straightforward way to remember this? Thanks

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

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

Post by admin »

OCAJO1 wrote:
Sun Dec 16, 2018 4:23 pm
It cares because when the subclass' constructor calls the superclass' constructor (not withstanding the case in Note) the superclass' constructor has to have the same or higher class of exception compare to the subclass' constructor.
It was a rhetorical question. Superclass doesn't care about the subclass simply because superclass is not dependent upon the subclass, subclass is dependent upon the superclass. So, superclass's constructor doesn't have to have anything based on the subclass's constructor. You are going in reverse that is why you are finding it confusing.

The logic is simple - A subclass constructor must always invoke one of the superclass's constructor (directly using super(...) or indirectly using this(...)) and this invocation must be on the first line of code in the constructor. If superclass's constructor says that it throws a particular checked exception, then the subclass constructor has no way to catch that exception. Therefore, it must propagate that exception to the caller and that is why it must declare that exception in its throws clause as well. Thus, if superclass's constructor declares that it throws an Exception E, then the subclass constructor must declare the same exception E or a wider excception i.e. superclass of E in its throws clause.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post by OCAJO1 »

So the subclass constructor has to have same or wider checked exception(s) than its superclass constructor, because unlike an overridden method (due to being the first line of code in the subclass), all it can do is to pass the checked exception(s) from the superclass' constructor down the line as it were.

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

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

Post by admin »

That is correct.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 0 guests