Page 1 of 1

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

Posted: Tue Oct 15, 2013 3:25 pm
by morris
Explanation:
Note that calling super() will not always work because if the super class has defined a constructor with arguments and has not defined a no args constructor then no args constructor will not be provided by the compiler. It is provided only to the class that does not define ANY constructor explicitly.

I don't agree with "super(...) can only be called in the first line of the constructor but this(...) can be called from anywhere." beeing a wrong answer.
It's never mentioned that we talk about no-args consturctors, or arguments in general.

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

Posted: Tue Oct 15, 2013 5:54 pm
by admin
Hi Morris,
This option is wrong irrespective of the number of arguments in the constructor. You don't need to know the number of arguments to say that this option is wrong.

HTH,
Paul.

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

Posted: Fri Jun 20, 2014 11:35 am
by sarakh
Hi guys,

What is super(...) and this(...)?
Does it mean that they can take any number of arguments?
Or does it mean that they take in an array?

and why is it that
"You can either call super(...) or this(...) but not both."?

Thank you,
Sarakh

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

Posted: Fri Jun 20, 2014 11:41 am
by admin
In this case, it just means that you want to call super or this with the correct number of parameters (whatever they are in a given situation).

HTH,
Paul.

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

Posted: Fri Jun 20, 2014 11:45 am
by sarakh
Hi Paul,
Thank you for the reply.
and why is it that
"You can either call super(...) or this(...) but not both."?
So I can not have a method that says this(...) and then super(...)??

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

Posted: Fri Jun 20, 2014 11:47 am
by admin
That is correct. The following would be wrong:
void someConstructor(params){
super(params);
this(params);
}

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

Posted: Fri Jun 20, 2014 11:49 am
by sarakh
But is it ok like this:
void someConstructor(){
super();
this();
}
without params?

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

Posted: Fri Jun 20, 2014 5:43 pm
by admin
Did you try it out?

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

Posted: Sat Jun 21, 2014 6:51 am
by sarakh
thank you for pushing me to try. I learned other good things along the way!

To come back to this one.

You can never have both this() and super().

Regardless of
void someConstructor(params){
super(params);
this(params);
}
or
void someConstructor(){
super();
this();
}
or any other combination.

It is wrong because both this() and super() want to be the first line. So it won't compile.

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

Posted: Sat Jun 21, 2014 7:16 am
by admin
Great! BTW, there is no difference between the semantics of a method (or constructor) with params and a method without params. Rules are same for both. In other words, when you says method(params), method() is included in it.

HTH,
Paul.

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

Posted: Sat Sep 27, 2014 11:06 am
by jbilkes
sarakh wrote:Hi Paul,
Thank you for the reply.
and why is it that
"You can either call super(...) or this(...) but not both."?
So I can not have a method that says this(...) and then super(...)??
Good question like most in this tool, very informing.

Nevertheless:

sarakh asked for a method not a constructor. Also, in the question all options except no. 5 mention a constructor.

In other words. The question at option 5 should be changed in mentioning a constructor. At least this is the case if it is correct that in a method which is NOT a constructor one is allowed to call super(...) and this(...) together. Another option would of course be raise the number of correct answers to 3 ;-)

plz tell me that im wrong, if only because i seem to be the first mentioning this issue

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

Posted: Sat Sep 27, 2014 11:21 am
by admin
Calls to super(...) and this(...) are applicable only in a constructor. Neither can be called in a method anyway. Now, within a constructor, only either of them can be called. Not both.
I agree that option 5 should make it clear that it is about a constructor.

HTH,
Paul.

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

Posted: Sun Aug 23, 2015 8:28 am
by mandavalliranjit
If a subclass does not have any declared constructors, the implicit default constructor of the subclass will have a call to super( ).

Does this not happen by default?

Yes it is true if the super class declares a constructor with arguments, the implicit default constructor of the subclass will have a call to super(... ). But how we just assume the super class has a constructor with arguments?

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

Posted: Sun Aug 23, 2015 9:25 am
by admin
mandavalliranjit wrote:If a subclass does not have any declared constructors, the implicit default constructor of the subclass will have a call to super( ).
Yes, that is correct.
Does this not happen by default?
Not sure what you mean.
Yes it is true if the super class declares a constructor with arguments, the implicit default constructor of the subclass will have a call to super(... ). But how we just assume the super class has a constructor with arguments?
The default constructor supplied by the compiler will not have a called to super(...). It will have a call to exactly super(). i.e. without any parameters.
If the superclass does not have such a constructor, compilation of the subclass will fail.
You should try this out.

HTH,
Paul.