Page 1 of 1

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

Posted: Wed Apr 11, 2012 12:23 pm
by ETS User
This question is missing text for answer options 2, 3, and 4.

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

Posted: Wed Apr 11, 2012 7:53 pm
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 9626 times

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

Posted: Thu Jan 24, 2013 3:42 pm
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

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

Posted: Thu Jan 24, 2013 3:47 pm
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.

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

Posted: Fri Jan 25, 2013 9:34 am
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.

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

Posted: Fri Jan 25, 2013 10:08 am
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.

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

Posted: Sun Jul 13, 2014 4:19 pm
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?

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

Posted: Sun Jul 13, 2014 10:04 pm
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.

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

Posted: Mon Apr 20, 2015 11:52 am
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

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

Posted: Mon Apr 20, 2015 8:30 pm
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.

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

Posted: Wed Apr 22, 2015 1:01 pm
by dmcinnis1
thanks.

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

Posted: Fri Oct 02, 2015 10:42 am
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.

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

Posted: Thu Apr 15, 2021 3:20 pm
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?

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

Posted: Thu Apr 15, 2021 6:28 pm
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.