About Question enthuware.ocpjp.v7.2.1113 :

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

Moderator: admin

Post Reply
leorbarbosa
Posts: 17
Joined: Wed Nov 26, 2014 1:59 pm
Contact:

About Question enthuware.ocpjp.v7.2.1113 :

Post by leorbarbosa »

If in the one-arg constructor of the subclass, one-arg constructor of the super-class is called, why should I have to explicitly define a no args constructor in the super-class?

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

Re: About Question enthuware.ocpjp.v7.2.1113 :

Post by admin »

You don't have to. But is that the only constructor in the subclass?
If you like our products and services, please help us by posting your review here.

thodoris.bais
Posts: 25
Joined: Sat Jun 03, 2017 4:56 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1113 :

Post by thodoris.bais »

@admin if we don't have to, then why 4th option is also correct?

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

Re: About Question enthuware.ocpjp.v7.2.1113 :

Post by admin »

Because even though the one argument constructor of the given SubClass code does not require it, the two argument constructor of SubClass requires it.

My response above was specific to the question posed by the leo.
If you like our products and services, please help us by posting your review here.

thodoris.bais
Posts: 25
Joined: Sat Jun 03, 2017 4:56 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1113 :

Post by thodoris.bais »

admin wrote:Because even though the one argument constructor of the given SubClass code does not require it, the two argument constructor of SubClass requires it.

My response above was specific to the question posed by the leo.
How does the two argument constructor of SubClass require it?
Could you please elaborate? I cannot get it.

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

Re: About Question enthuware.ocpjp.v7.2.1113 :

Post by admin »

thodoris.bais wrote:
admin wrote:Because even though the one argument constructor of the given SubClass code does not require it, the two argument constructor of SubClass requires it.

My response above was specific to the question posed by the leo.
How does the two argument constructor of SubClass require it?
Could you please elaborate? I cannot get it.
Rule 1: The first line of every constructor must either be a call to its super class's constructor (using the super(...) syntax) or to another of its own constructors (using the this(...) syntax).

Rule 2: If a class's constructor doesn't explicitly follow rule 1, the compiler automatically adds the a call to the no args constructor of is super class. In other words, the compiler automatically inserts super(); in the code.

In this question, the code for two args constructor of subclass is :
public SubClass( int m, int n ) { i = m ; j = m ; } //1

You will notice that it does not follow rule 1. Therefore, compiler will add the call to super() as per rule 2.

This is OCAJP stuff. You should have learnt this while preparing for the OCA exam :)

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

thodoris.bais
Posts: 25
Joined: Sat Jun 03, 2017 4:56 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1113 :

Post by thodoris.bais »

admin wrote:
thodoris.bais wrote:
admin wrote:Because even though the one argument constructor of the given SubClass code does not require it, the two argument constructor of SubClass requires it.

My response above was specific to the question posed by the leo.
How does the two argument constructor of SubClass require it?
Could you please elaborate? I cannot get it.
Rule 1: The first line of every constructor must either be a call to its super class's constructor (using the super(...) syntax) or to another of its own constructors (using the this(...) syntax).

Rule 2: If a class's constructor doesn't explicitly follow rule 1, the compiler automatically adds the a call to the no args constructor of is super class. In other words, the compiler automatically inserts super(); in the code.

In this question, the code for two args constructor of subclass is :
public SubClass( int m, int n ) { i = m ; j = m ; } //1

You will notice that it does not follow rule 1. Therefore, compiler will add the call to super() as per rule 2.

This is OCAJP stuff. You should have learnt this while preparing for the OCA exam :)

HTH,
Paul.
I'm OK with Rule 2, but again, why do we need to explicitly define/hand-write a no-args constructor in the SuperClass? Isn't gonna be automatically provided from the compiler?

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

Re: About Question enthuware.ocpjp.v7.2.1113 :

Post by admin »

1. You need a one args constructor in the super class because it is being called explicitly at //2.
2. Once you add a one arg constructor in the superclass, the compiler will not automatically provide a no-args constructor anymore. You will now need to write that explicitly. This is also OCAJP stuff :)
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 25 guests