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

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

Moderator: admin

Post Reply
Zoryanat
Posts: 20
Joined: Tue Aug 27, 2013 3:16 am
Contact:

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

Post by Zoryanat »

How come you can have two constructors with the same signature? In this case class B would have two constructors both of which take in an int, how would compiler know which one to use?....

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

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

Post by admin »

You cannot have two constructors (or even two methods) with the same signature in the same class. Where do you see that happening in this question?

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

mozilla20
Posts: 1
Joined: Tue Dec 24, 2013 9:36 am
Contact:

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

Post by mozilla20 »

I think the idea is, you can't apply both of the constructors simultaneously to Class B, otherwise, you would have two constructors with the same signature.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

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

Post by gparLondon »

Do you mean choice E constructor calling this will not have implicit call to super?

i.e option E B(int Z){super(); this(z,z)} will this not happen at compile time?

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

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

Post by admin »

gparLondon wrote:Do you mean choice E constructor calling this will not have implicit call to super?

i.e option E B(int Z){super(); this(z,z)} will this not happen at compile time?
That is correct.
If you like our products and services, please help us by posting your review here.

subhamsdalmia
Posts: 32
Joined: Sat May 02, 2015 11:57 pm
Contact:

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

Post by subhamsdalmia »

Didnt understand the question!

class A{
int i;
public A(int x) { this.i = x; }
}
class B extends A{
int j;
public B(int x, int y) { super(x); this.j = y; }
}

compiles without err. in eclipse,
after adding main,


public class SuperClass {
int i;

public SuperClass(int x) {this.i=x;}
public static void main(String[] args) {}
}

class SubClass extends SuperClass {
int j;
public SubClass (int x , int y) { super (x); this.j = y;}
}

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

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

Post by admin »

The question asks which of the constructors given in the options can still be added to class B. It doesn't say that the existing code does or doesn't compile.
If you like our products and services, please help us by posting your review here.

ashutoshkhare21
Posts: 1
Joined: Sat Oct 24, 2015 12:47 am
Contact:

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

Post by ashutoshkhare21 »

Just to clarify..
this.j is implicit in 3rd option. Right?

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

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

Post by admin »

There is no other j available in the scope. So yes, compiler will try to see if this.j is valid and it will find that it is indeed valid.
If you like our products and services, please help us by posting your review here.

Rinkesh
Posts: 35
Joined: Sat Nov 25, 2017 4:13 pm
Contact:

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

Post by Rinkesh »

this(z,z) will call public B(int x,int y) which has super(x) which further calls public A(int x),Am I right?

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

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

Post by admin »

Rinkesh wrote:this(z,z) will call public B(int x,int y) which has super(x) which further calls public A(int x),Am I right?
Correct.
If you like our products and services, please help us by posting your review here.

swarna pakeer
Posts: 16
Joined: Thu Mar 19, 2020 2:27 pm
Contact:

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

Post by swarna pakeer »

question says "which of the constructors shown in the options can be added to class B without causing a compilation to fail?" , which means even after adding the 2 constructors from options , the program should compile fine right? but if we add option c and E constructors ,it will not compile because both methods have same name and type . please clarify.

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

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

Post by admin »

No, you have to select 2 options. So, it is clear that you cannot have both of them at the same time otherwise, as you said, it will not compile!

Even from the options it is clear that if you add any two options in the class together, it will not compile, while the problem statement is asking you to ensure success in compilation. So, they have to be inserted independent of each other.
If you like our products and services, please help us by posting your review here.

Ph03nix89
Posts: 1
Joined: Sat Aug 22, 2020 1:03 pm
Contact:

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

Post by Ph03nix89 »

Quick question, how are classes A and B compiling with the absense of a no-args constructor?

Because both classes declare constructors with arguments is it not necessary to declare a no-args one as it won't be done automatically?

Apologies if this is obvious but I've read the below answers and the explanation and this particular point still isn't clicking for me.

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

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

Post by admin »

It is not necessary for a class to have a no args constructor. A class needs a no args constructor only if you want to instantiate it without any arguments.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 78 guests