About Question enthuware.ocajp.i.v7.2.1355 :
Moderator: admin
-
- Posts: 20
- Joined: Tue Aug 27, 2013 3:16 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1355 :
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?....
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1355 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Tue Dec 24, 2013 9:36 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1355 :
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.
-
- Posts: 63
- Joined: Fri Oct 31, 2014 6:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1355 :
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?
i.e option E B(int Z){super(); this(z,z)} will this not happen at compile time?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1355 :
That is correct.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?
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Sat May 02, 2015 11:57 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1355 :
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;}
}
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;}
}
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1355 :
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.
-
- Posts: 1
- Joined: Sat Oct 24, 2015 12:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1355 :
Just to clarify..
this.j is implicit in 3rd option. Right?
this.j is implicit in 3rd option. Right?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1355 :
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.
-
- Posts: 35
- Joined: Sat Nov 25, 2017 4:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1355 :
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?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1355 :
Correct.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?
If you like our products and services, please help us by posting your review here.
-
- Posts: 16
- Joined: Thu Mar 19, 2020 2:27 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1355 :
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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1355 :
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.
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.
-
- Posts: 1
- Joined: Sat Aug 22, 2020 1:03 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1355 :
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.
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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1355 :
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.
Who is online
Users browsing this forum: No registered users and 72 guests