About Question enthuware.ocajp.i.v7.2.1209 :

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

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocajp.i.v7.2.1209 :

Post by ETS User »

"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.

dtchky
Posts: 19
Joined: Wed Aug 01, 2012 3:11 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by dtchky »

Why not test it yourself and see :-) Write some code.
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?
Explicitly, yes, one additional constructor is invoked in class C by the call to this() in C(String s) {...}

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.
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.
Java is telling the truth, the guy you quote above is wrong.

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

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by admin »

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!
If you like our products and services, please help us by posting your review here.

AnotherGuest

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by AnotherGuest »

Quick question, won't line //C3 cause problems?

Code: Select all

public C(int i){} // C3
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 :)

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

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by admin »

AnotherGuest wrote:Quick question, won't line //C3 cause problems?

Code: Select all

public C(int i){} // C3
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 :)
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:

Code: Select all

public C(int i){ super(); } // C3
So basically, you can see that it does not need a constructor that takes in int in the super class.

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

Guest

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by Guest »

admin wrote:
AnotherGuest wrote:Quick question, won't line //C3 cause problems?

Code: Select all

public C(int i){} // C3
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 :)
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:

Code: Select all

public C(int i){ super(); } // C3
So basically, you can see that it does not need a constructor that takes in int in the super class.

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){} 
, that constructor doesn't exist.

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.

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

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by admin »

Guest wrote:
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){} 
, that constructor doesn't exist.

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.
Did you read the explanation to option 4?
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.

deniscapeto
Posts: 6
Joined: Wed Dec 02, 2015 5:50 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by deniscapeto »

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.

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

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by admin »

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.
:D Yes, you are right.
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.

Denyo1986
Posts: 38
Joined: Thu Jan 07, 2021 2:47 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by Denyo1986 »

admin wrote:
Wed Sep 12, 2012 6:51 pm
AnotherGuest wrote:Quick question, won't line //C3 cause problems?

Code: Select all

public C(int i){} // C3
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 :)
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:

Code: Select all

public C(int i){ super(); } // C3
So basically, you can see that it does not need a constructor that takes in int in the super class.

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.

Denyo1986
Posts: 38
Joined: Thu Jan 07, 2021 2:47 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by Denyo1986 »

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....)

Denyo1986
Posts: 38
Joined: Thu Jan 07, 2021 2:47 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by Denyo1986 »

Denyo1986 wrote:
Tue Jan 12, 2021 8:20 am
admin wrote:
Wed Sep 12, 2012 6:51 pm
AnotherGuest wrote:Quick question, won't line //C3 cause problems?

Code: Select all

public C(int i){} // C3
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 :)
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:

Code: Select all

public C(int i){ super(); } // C3
So basically, you can see that it does not need a constructor that takes in int in the super class.

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...;)

Denyo1986
Posts: 38
Joined: Thu Jan 07, 2021 2:47 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by Denyo1986 »

Denyo1986 wrote:
Tue Jan 12, 2021 8:25 am
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....)
this one remains though

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

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by admin »

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.

Denyo1986
Posts: 38
Joined: Thu Jan 07, 2021 2:47 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by Denyo1986 »

admin wrote:
Tue Jan 12, 2021 9:33 am
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.
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?

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

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

Denyo1986
Posts: 38
Joined: Thu Jan 07, 2021 2:47 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by Denyo1986 »

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.

baichen7788
Posts: 23
Joined: Fri Mar 26, 2021 7:25 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by baichen7788 »

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. "

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

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by admin »

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.

qazwsx922
Posts: 7
Joined: Sat Sep 04, 2021 8:17 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by qazwsx922 »

Code: Select all

private C(){ super(); } // C1
    public C(String s){  this();  System.out.println("C :"+s);  } // C2
In C2, 'this()' refers to object itself so C. right?
My question is, then in C1, what does C1 call? is it B or A? or both of them?

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

Re: About Question enthuware.ocajp.i.v7.2.1209 :

Post by admin »

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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 47 guests