About Question enthuware.ocpjp.v7.2.1113 :
Moderator: admin
-
- Posts: 17
- Joined: Wed Nov 26, 2014 1:59 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1113 :
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?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1113 :
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.
-
- Posts: 25
- Joined: Sat Jun 03, 2017 4:56 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1113 :
@admin if we don't have to, then why 4th option is also correct?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1113 :
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.
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.
-
- Posts: 25
- Joined: Sat Jun 03, 2017 4:56 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1113 :
How does the two argument constructor of SubClass require it?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.
Could you please elaborate? I cannot get it.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1113 :
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).thodoris.bais wrote:How does the two argument constructor of SubClass require it?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.
Could you please elaborate? I cannot get it.
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.
-
- Posts: 25
- Joined: Sat Jun 03, 2017 4:56 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1113 :
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 wrote: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).thodoris.bais wrote:How does the two argument constructor of SubClass require it?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.
Could you please elaborate? I cannot get it.
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.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1113 :
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
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.
Who is online
Users browsing this forum: No registered users and 47 guests