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

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

Moderator: admin

Post Reply
morris
Posts: 2
Joined: Tue Oct 15, 2013 3:19 pm
Contact:

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

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

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

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

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

sarakh
Posts: 23
Joined: Fri Jun 20, 2014 3:12 am
Contact:

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

Post 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

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

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

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

sarakh
Posts: 23
Joined: Fri Jun 20, 2014 3:12 am
Contact:

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

Post 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(...)??

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

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

Post by admin »

That is correct. The following would be wrong:
void someConstructor(params){
super(params);
this(params);
}
If you like our products and services, please help us by posting your review here.

sarakh
Posts: 23
Joined: Fri Jun 20, 2014 3:12 am
Contact:

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

Post by sarakh »

But is it ok like this:
void someConstructor(){
super();
this();
}
without params?

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

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

Post by admin »

Did you try it out?
If you like our products and services, please help us by posting your review here.

sarakh
Posts: 23
Joined: Fri Jun 20, 2014 3:12 am
Contact:

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

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

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

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

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

jbilkes
Posts: 21
Joined: Tue Sep 09, 2014 3:28 pm
Contact:

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

Post 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

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

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

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

mandavalliranjit
Posts: 1
Joined: Sun Aug 23, 2015 8:24 am
Contact:

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

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

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

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

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