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

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

Moderator: admin

Post Reply
fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post 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

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

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

Post by admin »

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.

aida.alemu
Posts: 3
Joined: Mon Mar 31, 2014 10:37 am
Contact:

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

Post 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

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

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

Post by admin »

Yes, that is correct.
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.896 :

Post by AndaRO »

What do you mean "nullary constructor"?
Is no args constructor?

Thank in advance Paul

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

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

Post by admin »

AndaRO wrote:What do you mean "nullary constructor"?
Is no args constructor?
Yes, see example above.
If you like our products and services, please help us by posting your review here.

JuergGogo
Posts: 28
Joined: Mon Sep 25, 2017 8:16 am
Contact:

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

Post 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.

kevin35
Posts: 8
Joined: Mon Dec 25, 2017 4:30 am
Contact:

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

Post 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 {
	}
}

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

kevin35
Posts: 8
Joined: Mon Dec 25, 2017 4:30 am
Contact:

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

Post 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

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests