About Question enthuware.ocpjp.v7.2.1111 :
Moderator: admin
About Question enthuware.ocpjp.v7.2.1111 :
Who can explain why the code doesn't need to have a constructor without parameters in class B to be compiled without errors?
Online
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1111 :
Class B does need a no-args constructor because the constructors of class C depend on that. But since class B does not define any constructor explicitly, the compiler provides the no args constructor to class B automatically.
As the explanation says, "Because //B1 is not a constructor. Note that it is returning an int."
HTH,
Paul.
As the explanation says, "Because //B1 is not a constructor. Note that it is returning an int."
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocpjp.v7.2.1111 :
Yeah! How I couldn't notice that! Class B does not provide any constructor explicitly...
Thanks!
Thanks!
-
- Posts: 1
- Joined: Sat Oct 26, 2013 4:14 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1111 :
"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."
But how that does work with constructor overloading and this();?
Is it then not correct to say that two constructors were called for that class? One explicitly and one implicitly?
To create any object one and only one constructor of that class and each of the super classes is called."
But how that does work with constructor overloading and this();?
Is it then not correct to say that two constructors were called for that class? One explicitly and one implicitly?
Online
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1111 :
Calling this(...) is not much different from calling any method in a constructor. The explanation is talking about constructors that are called by the JVM as a result of doing new on a Class. The JVM calls exactly one constructor.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Fri Jun 20, 2014 3:12 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1111 :
The code says
And the explanation says
1. b1 == false
2. b2 = result of 1
we also have
enthuware.ocajp.i.v7.2.949
with code
and explanation
1. b2 != b1
2. result of 1 = !b2
So if I want to talk about the order of evaluation of =, != and ==, can I say:
- first != or == is evaluated, whichever comes first in the code
- then = is evaluated.
Is that correct?
Code: Select all
boolean b1 = false;
boolean b2 = false;
if (b2 = b1 == false){ System.out.println("true"); }
else{ System.out.println("false"); }
So the order of execution is:All that if() needs is a boolean, now b1 == false returns true, which is a boolean and since b2 = true is an expression and every expression has a return value (which is the Left Hand Side of the expression), it returns true, which is again a boolean. FYI: the return value of expression i = 10; is 10 (an int).
1. b1 == false
2. b2 = result of 1
we also have
enthuware.ocajp.i.v7.2.949
with code
Code: Select all
boolean b1 = false;
boolean b2 = false;
if (b2 != b1 = !b2){
System.out.println("true");
}
else{
System.out.println("false");
}
So the order of execution is:Note that boolean operators have more precedence than =. (In fact, = has least precedence of all operators.)
so, in (b2 != b1 = !b2) first b2 != b1 is evaluated which returns a value 'false'. So the expression becomes false = !b2. And this is illegal because false is a value and not a variable!
1. b2 != b1
2. result of 1 = !b2
So if I want to talk about the order of evaluation of =, != and ==, can I say:
- first != or == is evaluated, whichever comes first in the code
- then = is evaluated.
Is that correct?
Online
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1111 :
Yes, that is correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 97
- Joined: Wed Dec 28, 2016 9:00 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1111 :
I was surprised to see the Code compilation fails to be incorrect only to find later that
the line
public int B(String s) { System.out.println("B :"+s); return 0; } // B1
is valid!! I think you should also add the explanation for this line too! Since it isn't read the following link:
http://stackoverflow.com/questions/3401 ... ructor-why
the line
public int B(String s) { System.out.println("B :"+s); return 0; } // B1
is valid!! I think you should also add the explanation for this line too! Since it isn't read the following link:
http://stackoverflow.com/questions/3401 ... ructor-why
Online
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1111 :
It does include an explanation under this option that tells that it is valid method and not a constructor.
If you like our products and services, please help us by posting your review here.
-
- Posts: 97
- Joined: Wed Dec 28, 2016 9:00 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1111 :
Yes it does. But it doesn't say that such methods are allowed.admin wrote:It does include an explanation under this option that tells that it is valid method and not a constructor.
Also the below code also compiles which is suprising to me:
public B()
{
}
public int B() { System.out.println("B :"); return 0; } // B1
}
Great question btw!
Online
-
- Site Admin
- Posts: 10045
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1111 :
If something is valid doesn't that mean it is allowed? It is allowed that is why it is valid, no?
If you like our products and services, please help us by posting your review here.
-
- Posts: 97
- Joined: Wed Dec 28, 2016 9:00 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1111 :
True I was lost somewhere during this test I guess to not follow the implications.admin wrote:If something is valid doesn't that mean it is allowed? It is allowed that is why it is valid, no?
Who is online
Users browsing this forum: No registered users and 11 guests