About Question enthuware.ocajp.i.v7.2.1129 :
Moderator: admin
-
- Posts: 4
- Joined: Sat Oct 13, 2012 2:00 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
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
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
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
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.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
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.
-
- Posts: 17
- Joined: Fri Sep 06, 2013 1:37 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
It seems to me, that I don't understand something. In the section "Explanation" read as follows
Does the phraseAs PortConnector has only one constructor, ...
imply that other constructors are prohibited inside this valid code? Frankly speaking I thought that other constructors are allowed ....other valid code.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
"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.
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.
-
- Posts: 17
- Joined: Fri Sep 06, 2013 1:37 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
Ok, it seems to me that now I get it. Thank you!
-
- Posts: 7
- Joined: Mon Jan 27, 2014 6:25 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
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.
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
Please read my post here: viewtopic.php?f=2&t=2319&p=6892convertor wrote: ...
our previous code where we catch IOException and met suddenly Exception, will not work.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Mon Jan 27, 2014 6:25 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
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
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
-
- Posts: 14
- Joined: Sat Mar 01, 2014 9:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
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)."
"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)."
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
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.
-
- Posts: 36
- Joined: Tue May 06, 2014 8:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
I don't believe the explanation for option#6 is entirely correct.
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.
I thought an overriding method in the subclass can through the same checked exception or a subset and it can throw any unchecked exception.An overriding method cannot throw a superclass 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{ }
}
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
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.
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.
-
- Posts: 5
- Joined: Sun Aug 31, 2014 5:44 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
Answer 1:
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:
Chris
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 not possible to define CleanConnector that does not throw IOException at instantiation.
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:
Cheers!It is not possible to define CleanConnector that throws IOException at instantiation.
Chris
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
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.
-
- Posts: 24
- Joined: Wed Sep 02, 2015 3:43 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
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
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
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
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.
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.
-
- Posts: 31
- Joined: Wed Feb 08, 2017 5:42 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
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.
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.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
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?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
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.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
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.
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
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??
>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.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
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
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
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
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.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1129 :
That is correct.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 102 guests