About Question enthuware.ocajp.i.v7.2.1256 :
Moderator: admin
-
- Posts: 2
- Joined: Tue Oct 15, 2013 3:19 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1256 :
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.
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.
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1256 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Fri Jun 20, 2014 3:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1256 :
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
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
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1256 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Fri Jun 20, 2014 3:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1256 :
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(...)??
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(...)??
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1256 :
That is correct. The following would be wrong:
void someConstructor(params){
super(params);
this(params);
}
void someConstructor(params){
super(params);
this(params);
}
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Fri Jun 20, 2014 3:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1256 :
But is it ok like this:
void someConstructor(){
super();
this();
}
without params?
void someConstructor(){
super();
this();
}
without params?
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1256 :
Did you try it out?
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Fri Jun 20, 2014 3:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1256 :
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.
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.
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1256 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 21
- Joined: Tue Sep 09, 2014 3:28 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1256 :
Good question like most in this tool, very informing.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(...)??
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
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1256 :
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.
I agree that option 5 should make it clear that it is about a constructor.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Sun Aug 23, 2015 8:24 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1256 :
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?
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?
-
- Site Admin
- Posts: 10113
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1256 :
Yes, that is correct.mandavalliranjit wrote:If a subclass does not have any declared constructors, the implicit default constructor of the subclass will have a call to super( ).
Not sure what you mean.Does this not happen by default?
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.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?
If the superclass does not have such a constructor, compilation of the subclass will fail.
You should try this out.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 16 guests