Page 1 of 1

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

Posted: Tue Mar 25, 2014 10:08 pm
by fasty23
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

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

Posted: Wed Mar 26, 2014 12:27 am
by admin

Code: Select all

public class Base{
 public Base() throws Exception{
 }
}

class Sub extends Base{ // will not compile.
}

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

Posted: Wed Apr 02, 2014 11:35 am
by aida.alemu
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

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

Posted: Wed Apr 02, 2014 8:33 pm
by admin
Yes, that is correct.

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

Posted: Fri Feb 24, 2017 5:36 pm
by AndaRO
What do you mean "nullary constructor"?
Is no args constructor?

Thank in advance Paul

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

Posted: Fri Feb 24, 2017 10:44 pm
by admin
AndaRO wrote:What do you mean "nullary constructor"?
Is no args constructor?
Yes, see example above.

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

Posted: Mon Oct 02, 2017 4:54 am
by JuergGogo
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.

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

Posted: Tue Jan 09, 2018 1:45 pm
by kevin35
admin wrote:

Code: Select all

public class Base{
 public Base() throws Exception{
 }
}

class Sub extends Base{ // will not compile.
}
Hi Paul, can you explain why Sub must have a constructor that throws Exception like

Code: Select all

class Sub extends Base{ 
 Sub () throws Exception{
 }
}
For method you can omit throwing Exception when overriding
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 {
	}
}

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

Posted: Tue Jan 09, 2018 9:26 pm
by admin
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.

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

Posted: Wed Jan 10, 2018 2:53 am
by kevin35
admin 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.
ah got it, tnx for explanation

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

Posted: Mon Apr 18, 2022 1:45 am
by sivasd
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?

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

Posted: Mon Apr 18, 2022 10:52 am
by admin
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".

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

Posted: Mon Apr 18, 2022 9:00 pm
by sivasd
admin wrote:
Mon Apr 18, 2022 10:52 am
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".
Thanks for the clarification