About Question enthuware.ocajp.i.v7.2.1209 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1209 :
"One of the constructors of each class is called as a result of constructing an object of class C."
"To create any object one and only one constructor of that class and each of the super classes is called."
class C extends B{
private C(){ super(); } // C1
public C(String s){ this(); System.out.println("C :"+s); } // C2
public C(int i){} // C3
}
If user call new C("string") , both the constructor C() and C(String s) would be invoked[because there is this() in the constructor C(String s) ].
Isn't two constructors in class C is called?
By the way, I've googled about it and I found the exactly same question in a forum:
http://www.coderanch.com/t/195212/java- ... ctors-help
The guy also think that "One of the constructors of each class is called as a result of constructing an object of class C." is false.
"To create any object one and only one constructor of that class and each of the super classes is called."
class C extends B{
private C(){ super(); } // C1
public C(String s){ this(); System.out.println("C :"+s); } // C2
public C(int i){} // C3
}
If user call new C("string") , both the constructor C() and C(String s) would be invoked[because there is this() in the constructor C(String s) ].
Isn't two constructors in class C is called?
By the way, I've googled about it and I found the exactly same question in a forum:
http://www.coderanch.com/t/195212/java- ... ctors-help
The guy also think that "One of the constructors of each class is called as a result of constructing an object of class C." is false.
-
- Posts: 19
- Joined: Wed Aug 01, 2012 3:11 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1209 :
Why not test it yourself and see
Write some code.
In order of invocation:
A()
B() // this is created and invoked automatically
C()
C(String s)
There are two (very) important things to consider with constructors:
* If you do not make a constructor, the default empty constructor is automatically created.
* If any constructor does not explicitly call a super or this constructor as its first statement, a call to super() is automatically added.
![Smile :-)](./images/smilies/icon_e_smile.gif)
Explicitly, yes, one additional constructor is invoked in class C by the call to this() in C(String s) {...}If user call new C("string") , both the constructor C() and C(String s) would be invoked[because there is this() in the constructor C(String s) ].
Isn't two constructors in class C is called?
In order of invocation:
A()
B() // this is created and invoked automatically
C()
C(String s)
There are two (very) important things to consider with constructors:
* If you do not make a constructor, the default empty constructor is automatically created.
* If any constructor does not explicitly call a super or this constructor as its first statement, a call to super() is automatically added.
Java is telling the truth, the guy you quote above is wrong.The guy also think that "One of the constructors of each class is called as a result of constructing an object of class C." is false.
-
- Site Admin
- Posts: 10076
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1209 :
As dtchky pointed out, the second constructor is not actually invoked automatically but is being explicitly called. Just like a constructor may call any other method.
But you are right, it makes the option explanation a little ambiguous and so it should be made clear what it means.
thank you!
But you are right, it makes the option explanation a little ambiguous and so it should be made clear what it means.
thank you!
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1209 :
Quick question, won't line //C3 cause problems?
It doesn't invoke any other constructors and there is no matching constructor in B (or A) that have an int argument, could you help clarify this for me please?
Thanks![Smile :)](./images/smilies/icon_e_smile.gif)
Code: Select all
public C(int i){} // C3
Thanks
![Smile :)](./images/smilies/icon_e_smile.gif)
-
- Site Admin
- Posts: 10076
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1209 :
When you don't call super class's constructor explicitly, the compiler adds the call to the super class's no-args constructor itself. This means, the compiler turns the above code into:AnotherGuest wrote:Quick question, won't line //C3 cause problems?
It doesn't invoke any other constructors and there is no matching constructor in B (or A) that have an int argument, could you help clarify this for me please?Code: Select all
public C(int i){} // C3
Thanks
Code: Select all
public C(int i){ super(); } // C3
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1209 :
admin wrote:When you don't call super class's constructor explicitly, the compiler adds the call to the super class's no-args constructor itself. This means, the compiler turns the above code into:AnotherGuest wrote:Quick question, won't line //C3 cause problems?
It doesn't invoke any other constructors and there is no matching constructor in B (or A) that have an int argument, could you help clarify this for me please?Code: Select all
public C(int i){} // C3
ThanksSo basically, you can see that it does not need a constructor that takes in int in the super class.Code: Select all
public C(int i){ super(); } // C3
HTH,
Paul.
Paul,
There is still a problem with this question. Even though the compiler *does* automatically insert the call to super() for
Code: Select all
public C(int i){}
In other words, there is NOT a no-args constructor in class B, which is the superclass of C. Look at this snippet:
Code: Select all
class B extends A{
public int B(String s) { System.out.println("B :"+s); return 0; } // B1
}
Clearly there isn't an explicit no-args constructor, and the compiler will not create an explicit one in this case.
-
- Site Admin
- Posts: 10076
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1209 :
Did you read the explanation to option 4?Guest wrote:
Paul,
There is still a problem with this question. Even though the compiler *does* automatically insert the call to super() for, that constructor doesn't exist.Code: Select all
public C(int i){}
In other words, there is NOT a no-args constructor in class B, which is the superclass of C. Look at this snippet:
Code: Select all
class B extends A{ public int B(String s) { System.out.println("B :"+s); return 0; } // B1 }
Clearly there isn't an explicit no-args constructor, and the compiler will not create an explicit one in this case.
public int B(String s) is NOT a constructor. Notice that it has a return type. So, effectively, there no explicit constructor defined for B. Therefore, a no-args constructor will be provided.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 6
- Joined: Wed Dec 02, 2015 5:50 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1209 :
am I missing something or we can answer this question without read the question?
if the option "The code will not compile. " is correct the others are wrong since we presume they compile.
However, if we have to select 4 options, the option "The code will not compile " is wrong.
Therefore, the others are correct.
if the option "The code will not compile. " is correct the others are wrong since we presume they compile.
However, if we have to select 4 options, the option "The code will not compile " is wrong.
Therefore, the others are correct.
-
- Site Admin
- Posts: 10076
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1209 :
deniscapeto wrote:am I missing something or we can answer this question without read the question?
if the option "The code will not compile. " is correct the others are wrong since we presume they compile.
However, if we have to select 4 options, the option "The code will not compile " is wrong.
Therefore, the others are correct.
![Very Happy :D](./images/smilies/icon_e_biggrin.gif)
Many users hide the number of correct answer while preparing. They cannot answer it this way though.
If you like our products and services, please help us by posting your review here.
-
- Posts: 38
- Joined: Thu Jan 07, 2021 2:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1209 :
admin wrote: ↑Wed Sep 12, 2012 6:51 pmWhen you don't call super class's constructor explicitly, the compiler adds the call to the super class's no-args constructor itself. This means, the compiler turns the above code into:AnotherGuest wrote:Quick question, won't line //C3 cause problems?
It doesn't invoke any other constructors and there is no matching constructor in B (or A) that have an int argument, could you help clarify this for me please?Code: Select all
public C(int i){} // C3
ThanksSo basically, you can see that it does not need a constructor that takes in int in the super class.Code: Select all
public C(int i){ super(); } // C3
HTH,
Paul.
And why is it not a problem that class B does not have a no-args constructor? Since one constructor is defined, there wont be any default constructor in class B.
Which constructor of its superclass (which is B) does the above mentioned constructor in class C call then? I dont get it.
-
- Posts: 38
- Joined: Thu Jan 07, 2021 2:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1209 :
And another topic/problem:
Option 3 (Class C can be instantiated only in two ways by users of class C.) is ambiguous in my opionion. What is a "user of class C"?
Me, the programmer, is a user of class C. While I am writing code within class C, I am using class C.
So the mere fact that I am USING this class does not say where I am standing (e.g. within the class or outside of the class).
I think the option should be rephrased to something like (From outside of the class, class C can be instantiated only....)
Option 3 (Class C can be instantiated only in two ways by users of class C.) is ambiguous in my opionion. What is a "user of class C"?
Me, the programmer, is a user of class C. While I am writing code within class C, I am using class C.
So the mere fact that I am USING this class does not say where I am standing (e.g. within the class or outside of the class).
I think the option should be rephrased to something like (From outside of the class, class C can be instantiated only....)
-
- Posts: 38
- Joined: Thu Jan 07, 2021 2:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1209 :
Denyo1986 wrote: ↑Tue Jan 12, 2021 8:20 amadmin wrote: ↑Wed Sep 12, 2012 6:51 pmWhen you don't call super class's constructor explicitly, the compiler adds the call to the super class's no-args constructor itself. This means, the compiler turns the above code into:AnotherGuest wrote:Quick question, won't line //C3 cause problems?
It doesn't invoke any other constructors and there is no matching constructor in B (or A) that have an int argument, could you help clarify this for me please?Code: Select all
public C(int i){} // C3
ThanksSo basically, you can see that it does not need a constructor that takes in int in the super class.Code: Select all
public C(int i){ super(); } // C3
HTH,
Paul.
And why is it not a problem that class B does not have a no-args constructor? Since one constructor is defined, there wont be any default constructor in class B.
Which constructor of its superclass (which is B) does the above mentioned constructor in class C call then? I dont get it.
FORGET IT...I missed the explanation. Got it now. didnt see that it is a method. God, you guys are good...
![Wink ;)](./images/smilies/icon_e_wink.gif)
-
- Posts: 38
- Joined: Thu Jan 07, 2021 2:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1209 :
this one remains thoughDenyo1986 wrote: ↑Tue Jan 12, 2021 8:25 amAnd another topic/problem:
Option 3 (Class C can be instantiated only in two ways by users of class C.) is ambiguous in my opionion. What is a "user of class C"?
Me, the programmer, is a user of class C. While I am writing code within class C, I am using class C.
So the mere fact that I am USING this class does not say where I am standing (e.g. within the class or outside of the class).
I think the option should be rephrased to something like (From outside of the class, class C can be instantiated only....)
-
- Site Admin
- Posts: 10076
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1209 :
Any class that makes use of class C other than class C itself (such as creating an object of C, calling a method on C, or accessing a field from another class) is the user of class C. Not sure where you find an ambiguity in this.
If you like our products and services, please help us by posting your review here.
-
- Posts: 38
- Joined: Thu Jan 07, 2021 2:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1209 :
The ambiguity is somewhere between your and my interpretation of what "a user" is.
Could you maybe link any reliable source that contains such an definition of a "user"?
I would consider a member (e.g. a method) of class C that calls a constructor of (its own) class C a "user" of this constructor. This member uses the code or functionality that the constructor provides. It calls it and gets something back. It uses it.
Does that not make sense to you? If not, why?
Maybe "user" is a clearly defined term in java that is clear to be OUTSIDE of the refered-to context. But I havent seen such a definition and therefore I apply my own understanding of "user", hence the question is ambiguous for me.
My question in return is:
Would it hurt to rephrase the question to make it unmistakably clear?
-
- Site Admin
- Posts: 10076
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1209 :
I am sorry I don't have any authoritative definition of "user" but it would be easy to make a distinction between the terms "user of a class" and "developer of a class" . Yes, a method of the same class could be the user of a constructor of that class.
One could certainly split hair if one wants to, but I think the meaning is quite clear in this question. I leave this thread here for more feedback. If more users get confused by this, we will change it.
One could certainly split hair if one wants to, but I think the meaning is quite clear in this question. I leave this thread here for more feedback. If more users get confused by this, we will change it.
If you like our products and services, please help us by posting your review here.
-
- Posts: 38
- Joined: Thu Jan 07, 2021 2:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1209 :
No, its not about splitting hairs. Please dont take it the wrong way.
Maybe try to see it from a student's perspective. With these kind of exams you get trained to be extremely vigilant, to double-question everything and to look for traps. So anything that is ambiguous in the slightest way is a distraction from the actual point of the question.
Its difficult enough already and its just frustrating if you dont score for a question which you could have mastered if the question would have been unmistakably clear.
At the end of the day, its just my feedback.
You are to decide if you want to act on it.
Anyhow, I appreciate the exchange on border topics to deepen my understanding of the language. So thank you.
Maybe try to see it from a student's perspective. With these kind of exams you get trained to be extremely vigilant, to double-question everything and to look for traps. So anything that is ambiguous in the slightest way is a distraction from the actual point of the question.
Its difficult enough already and its just frustrating if you dont score for a question which you could have mastered if the question would have been unmistakably clear.
At the end of the day, its just my feedback.
You are to decide if you want to act on it.
Anyhow, I appreciate the exchange on border topics to deepen my understanding of the language. So thank you.
-
- Posts: 23
- Joined: Fri Mar 26, 2021 7:25 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1209 :
Isn't is a compilation error because when instance C is created , a constructor of B is called , but B's constructor is not defined.
I remember the rule says "a super class must have constructor because super is called in subclass. "
I remember the rule says "a super class must have constructor because super is called in subclass. "
-
- Site Admin
- Posts: 10076
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1209 :
If a class does not define any constructor explicitly, the compiler creates one for it.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Sat Sep 04, 2021 8:17 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1209 :
Code: Select all
private C(){ super(); } // C1
public C(String s){ this(); System.out.println("C :"+s); } // C2
My question is, then in C1, what does C1 call? is it B or A? or both of them?
-
- Site Admin
- Posts: 10076
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1209 :
No, this() is a syntax to invoke the no-args constructor of class C on the current object. It does not refer to anything in the sense that references do. You can think of it like a method call. For example, if you have println("hello"), then println is not really a reference to any object. It is just name of a method.
I am not sure I understand what you mean by, "then in C1, what does C1 call? is it B or A? or both of them?".
There is a call to super() in C1. super() is the syntax to invoke the no-args constructor defined in the super class of the current class.
You seem to have some fundamental misunderstanding of basic concepts in OOP and Java and I suggest you to go through the theory from a good book such as OCAJP 8 Fundamentals by Hanumant Deshmukh before attempting the questions.
I am not sure I understand what you mean by, "then in C1, what does C1 call? is it B or A? or both of them?".
There is a call to super() in C1. super() is the syntax to invoke the no-args constructor defined in the super class of the current class.
You seem to have some fundamental misunderstanding of basic concepts in OOP and Java and I suggest you to go through the theory from a good book such as OCAJP 8 Fundamentals by Hanumant Deshmukh before attempting the questions.
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 31 guests