About Question enthuware.ocajp.i.v7.2.896 :
Moderator: admin
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
About Question enthuware.ocajp.i.v7.2.896 :
Would you please explain this sentence "It follows that if the nullary constructor of the superclass has a throws clause, then a compile-time error will occur. " in Explanation by an example.
tnx
tnx
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.896 :
Code: Select all
public class Base{
public Base() throws Exception{
}
}
class Sub extends Base{ // will not compile.
}
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Mon Mar 31, 2014 10:37 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.896 :
So does this mean that if a base class has a no args constructor that throws an exception, you can not extend it with a class that has no constructor because a no args default constructor will be created and this will make the code not compile? (because it tries to invoke the superclass constructor with the exception?)
Just to clarify...test tomorrow.
Thanks
Just to clarify...test tomorrow.
Thanks
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.896 :
Yes, that is correct.
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.896 :
What do you mean "nullary constructor"?
Is no args constructor?
Thank in advance Paul
Is no args constructor?
Thank in advance Paul
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.896 :
Yes, see example above.AndaRO wrote:What do you mean "nullary constructor"?
Is no args constructor?
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Mon Sep 25, 2017 8:16 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.896 :
I don't really agree that the constructor of class B is excactly the same as the default constructor, since the call to super() is missing. The compiler will add this call to super() though and the resulting constructor will be identical to the default constructor. I know this is hairsplitting, but this whole certification is just a tiny hair splitted in thousands of big pieces.
-
- Posts: 8
- Joined: Mon Dec 25, 2017 4:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.896 :
Hi Paul, can you explain why Sub must have a constructor that throws Exception likeadmin wrote:Code: Select all
public class Base{ public Base() throws Exception{ } } class Sub extends Base{ // will not compile. }
Code: Select all
class Sub extends Base{
Sub () throws Exception{
}
}
I know constructors are not methods, you can't override them.
I just don't understand the logic why Sub has to provide a constructor that throws Exception, super() gets called anyway.
The other way around compiles fine tho...
Code: Select all
public class Base {}
class Sub extends Base {
Sub() throws Exception {
}
}
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.896 :
Because the in case of a constructor, the base class constructor automatically calls the super class's constructor. If the super class's constructor says that it throws Exception, then obviously, the subclass constructor may receive that exception while invoking the super class's constructor, right? So it has to declare that exception in its throws clause also.
In case of method, the subclass method doesn't automatically invoke base class's method. So if it does not invoke base class's method, it does not need to have a throws clause.
In case of method, the subclass method doesn't automatically invoke base class's method. So if it does not invoke base class's method, it does not need to have a throws clause.
If you like our products and services, please help us by posting your review here.
-
- Posts: 8
- Joined: Mon Dec 25, 2017 4:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.896 :
ah got it, tnx for explanationadmin wrote:Because the in case of a constructor, the base class constructor automatically calls the super class's constructor. If the super class's constructor says that it throws Exception, then obviously, the subclass constructor may receive that exception while invoking the super class's constructor, right? So it has to declare that exception in its throws clause also.
-
- Posts: 4
- Joined: Mon Apr 18, 2022 1:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.896 :
The actual question is "Which of the following classes *have* a default constructor?"
In that case, both A and B have default constructors, so the answer should be A and B right?
In that case, both A and B have default constructors, so the answer should be A and B right?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.896 :
Actually, the phrase "default constructor" has a specific meaning in Java. It is the constructor that is provided by the compiler. This is as per Section 8.8.9 of JLS: f a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.
So, although class B does define a no-args constructor, it cannot be called the "default constructor".
So, although class B does define a no-args constructor, it cannot be called the "default constructor".
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Mon Apr 18, 2022 1:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.896 :
Thanks for the clarificationadmin wrote: ↑Mon Apr 18, 2022 10:52 amActually, the phrase "default constructor" has a specific meaning in Java. It is the constructor that is provided by the compiler. This is as per Section 8.8.9 of JLS: f a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.
So, although class B does define a no-args constructor, it cannot be called the "default constructor".
Who is online
Users browsing this forum: No registered users and 74 guests