Page 1 of 1

About Question enthuware.ocpjp.v7.2.1113 :

Posted: Fri Jun 03, 2016 12:53 pm
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?

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

Posted: Fri Jun 03, 2016 7:13 pm
by admin
You don't have to. But is that the only constructor in the subclass?

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

Posted: Sat Jun 03, 2017 5:13 pm
by thodoris.bais
@admin if we don't have to, then why 4th option is also correct?

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

Posted: Sat Jun 03, 2017 10:22 pm
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.

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

Posted: Thu Jun 08, 2017 5:09 pm
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.

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

Posted: Thu Jun 08, 2017 8:36 pm
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.

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

Posted: Fri Jun 09, 2017 5:00 am
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?

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

Posted: Fri Jun 09, 2017 5:11 am
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 :)