Page 1 of 1

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

Posted: Tue Oct 16, 2012 1:34 am
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

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

Posted: Tue Oct 16, 2012 6:29 am
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.

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

Posted: Mon Sep 09, 2013 4:21 am
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: .

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

Posted: Mon Sep 09, 2013 5:46 pm
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.

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

Posted: Tue Sep 10, 2013 2:49 am
by M.Komarov
Ok, it seems to me that now I get it. Thank you!

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

Posted: Tue Feb 04, 2014 12:38 pm
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.

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

Posted: Tue Feb 04, 2014 12:44 pm
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

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

Posted: Tue Feb 04, 2014 12:50 pm
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

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

Posted: Fri Mar 14, 2014 12:26 am
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)."

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

Posted: Fri Mar 14, 2014 12:44 am
by admin
Did you notice this part of the explanation that you've quoted, "...(or the super classes of the checked exceptions)"?

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

Posted: Fri Jun 27, 2014 9:13 am
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{ } 
}

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

Posted: Fri Jun 27, 2014 8:40 pm
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.

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

Posted: Tue Sep 02, 2014 12:01 am
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

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

Posted: Tue Sep 02, 2014 1:59 am
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.

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

Posted: Mon Oct 12, 2015 10:06 am
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 :)

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

Posted: Mon Oct 12, 2015 12:15 pm
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.

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

Posted: Mon Feb 27, 2017 9:15 am
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.

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

Posted: Thu Dec 13, 2018 8:33 pm
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?

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

Posted: Thu Dec 13, 2018 10:25 pm
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.

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

Posted: Fri Dec 14, 2018 7:19 pm
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.

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

Posted: Fri Dec 14, 2018 8:36 pm
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??

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

Posted: Sun Dec 16, 2018 4:23 pm
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

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

Posted: Sun Dec 16, 2018 9:34 pm
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.

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

Posted: Mon Dec 17, 2018 3:10 pm
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.

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

Posted: Mon Dec 17, 2018 9:21 pm
by admin
That is correct.