Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1355 :
Posted: Sun Dec 08, 2013 11:27 am
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?....
Re: About Question enthuware.ocajp.i.v7.2.1355 :
Posted: Sun Dec 08, 2013 9:26 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1355 :
Posted: Mon Dec 30, 2013 7:41 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1355 :
Posted: Thu Nov 06, 2014 2:44 pm
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?
Re: About Question enthuware.ocajp.i.v7.2.1355 :
Posted: Thu Nov 06, 2014 8:48 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1355 :
Posted: Mon May 04, 2015 3:08 am
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;}
}
Re: About Question enthuware.ocajp.i.v7.2.1355 :
Posted: Mon May 04, 2015 3:24 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1355 :
Posted: Sat Oct 24, 2015 12:50 am
by ashutoshkhare21
Just to clarify..
this.j is implicit in 3rd option. Right?
Re: About Question enthuware.ocajp.i.v7.2.1355 :
Posted: Sat Oct 24, 2015 3:08 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1355 :
Posted: Tue Dec 12, 2017 3:29 pm
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?
Re: About Question enthuware.ocajp.i.v7.2.1355 :
Posted: Tue Dec 12, 2017 10:32 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1355 :
Posted: Tue May 12, 2020 2:23 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1355 :
Posted: Tue May 12, 2020 2:38 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1355 :
Posted: Sat Aug 22, 2020 1:41 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1355 :
Posted: Sat Aug 22, 2020 5:55 pm
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.