About Question enthuware.ocpjp.v7.2.1113 :

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

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: 8703
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Fri Jun 03, 2016 7:13 pm

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 » Sat Jun 03, 2017 5:13 pm

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

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

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

Post by admin » Sat Jun 03, 2017 10:22 pm

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

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: 8703
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Thu Jun 08, 2017 8:36 pm

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

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: 8703
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Fri Jun 09, 2017 5:11 am

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: Bing [Bot] and 1 guest