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

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

Moderator: admin

Post Reply
ETS User

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

Post by ETS User »

This question is missing text for answer options 2, 3, and 4.

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

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

Post by admin »

This has now been fixed. Attached is the updated screen shot.

thank you very much for reporting this.
-Paul.
2.1261.gif
2.1261.gif (28.53 KiB) Viewed 9408 times
If you like our products and services, please help us by posting your review here.

The_Nick

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

Post by The_Nick »

Dear Enthuware developing team,
Why would be SuperClass(){

} a MUST as you specify in the exercise.

There is no reference to any superclass constructor with no args in the code. wouldn't it compile anyway?
Or should I have taken for granted that you implied that without SuperClass(){} constructor it would not be possible to instantiate a SuperClass object by declaring SuperClass class = new SuperClass(); which is quite clear.

Basically I do not understand why it's needed (it must exists) SuperClass(){} in old rder to compile.

I understand this explanation:
"The default no args constructor will not be provided because SuperClass has to define one arg constructor."
but it would be true only if there was an object of class SuperClass declared with the no-args constructor.
Or Am I wrong?

Thanks in advance for the clarification.

The_Nick

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

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

Post by admin »

Because the code at //1 implicitly uses the no args constructor of SuperClass. Whenever there is no explict call to the super class's constructor in a sub class's constructor, the compiler automatically inserts super() as the first line of the sub class's constructor.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

The_Nick

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

Post by The_Nick »

Ok thanks a lot for the immediate response.
However, do you know why? It couldn't be that the subclass would create his own no-args constructor?
Why the java creators (using the word "creators" to distinguish them from normal java developers) did implement such a functionality?

Thanks in advance.

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

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

Post by admin »

Since a subclass inherits the base class properties, to create a subclass instance, you have to initialize the base class properties, which is what constructors are for.

For example, lets say you have two classes Car and SportsCar, where SportsCar extends Car. Now, if you create a SportsCar object, all the properties of Car will also need to be initialized. In effect, you are implicitly creating a Car object too (because SportsCar is a Car). I am not saying there are two objects. There is only one SportsCar object but that object is of class Car as well. Therefore, it is necessary to invoke some constructor of the base class as well. So if the sub class doesn't do it explicitly then the compiler automatically inserts the call to no-args constructors.

Ideally, an explicit call to a super class's constructor should have been mandatory for the subclass constructors to avoid this confusion. But you can think of it as a shortcut that Java language creators have provided.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Brian B
Posts: 13
Joined: Mon Jun 16, 2014 1:07 pm
Contact:

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

Post by Brian B »

admin wrote:Because the code at //1 implicitly uses the no args constructor of SuperClass. Whenever there is no explict call to the super class's constructor in a sub class's constructor, the compiler automatically inserts super() as the first line of the sub class's constructor.
I'm confused when you stat that the code //1 implicitly...

Is this because 'k' is not initialized by the constructor at //1?

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

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

Post by admin »

No. Since the code at //1 doesn't call the super class's constructor explicitly, the compiler will automatically insert a call to super() as the first line of this constructor. It has nothing to do with k getting initialized or not.
If you like our products and services, please help us by posting your review here.

dmcinnis1
Posts: 16
Joined: Wed Feb 25, 2015 8:52 pm
Contact:

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

Post by dmcinnis1 »

Hi,

I assumed that the first Subclass constructor could call this(m), and then the no args constructor would not be required in the superclass. Am I overthinking it?

Thanks

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

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

Post by admin »

If the subclass called this(m), then it would be wrong because then it would be calling the same constructor i.e. it would be a recursive call.
You should try it out and see what happens.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

dmcinnis1
Posts: 16
Joined: Wed Feb 25, 2015 8:52 pm
Contact:

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

Post by dmcinnis1 »

thanks.

elissaf
Posts: 1
Joined: Fri Oct 02, 2015 10:21 am
Contact:

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

Post by elissaf »

It took me a bit to figure out why. When you don't define any constructors, the default 0-arg constructor is built for you. Once we call the 1-arg constructor however, we need to add that and, as a result, supply the 0-arg as well.

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

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

Post by enthunoob »

I thought a no-arg constructor was always added automatically if you didn't write it out yourself. From the explanations I understand that once a constructor with args is added, the no-arg constructor is not automatically added anymore, so it has to be explicitly written out in the code.

Do I understand it correctly?

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

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

Post by admin »

@enthunoob, yes, that is correct.
I would suggest you to go through a good book to cover the basics first before attempting the mock exams.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 37 guests