Forums Register Login

Some minor typos, chapter 5 (Java OCA 8 Programmer I Study Guide)

(1 cow) 1
+Pie Number of slices to send: Send
1. On page 246, “Overriding a Method” section, the third sentence:

“In this case, you can override a method a method by declaring a new method with the signature and return type as the method in the parent class”.


“a method” is repeated twice.

2. On page 247, the output of example:
Maybe you laugh at me for this note It doesn't actually matter but it is interesting. The outputs are 50.00 and 70.00 but we run this example the outputs would be 50.0 and 70.0. One zero is more than after dot.

3. Method signature;
On page 265, the last sentence:

"For example, the name and signature must be the same..."


and on page 278, the first sentence of second paragraph:

"If a class implements two interfaces that have default methods with the same name and signature, the compiler will throw an error".


Maybe "name" is redundant in the above sentences. Because in the last sentence on page 246 is written that signature includes the name.

"...the method signature includes the name and list of input parameters".


Maybe "name" should be "return type" on page 265.
1
+Pie Number of slices to send: Send
 

Mushfiq Mammadov wrote:1. On page 246, “Overriding a Method” section, the third sentence:


True! (But with duplicate "a method" you definitely know it's about methods and not fields )

Mushfiq Mammadov wrote:2. On page 247, the output of example:
Maybe you laugh at me for this note It doesn't actually matter but it is interesting. The outputs are 50.00 and 70.00 but we run this example the outputs would be 50.0 and 70.0. One zero is more than after dot.


Correct! And I would definitely not laugh about this one. 50.00 or 50.0 actually does matter: the 1st one has 4 significant figures, the latter one only 3.

Mushfiq Mammadov wrote:Maybe "name" is redundant in the above sentences. Because in the last sentence on page 246 is written that signature includes the name.


The method signature is indeed the combination of 2 components of the method declaration: the method's name and the parameter types. So the "name" part is indeed redundant, but it could also just be an additional aid to emphasize that the method name must be exactly the same (for those who have forgotten that method signature is name + parameters).

Mushfiq Mammadov wrote:Maybe "name" should be "return type" on page 265.


Here you are wrong! To have a valid override the return type must not be the same. Since Java 5 covariant return types are supported. Let's add a little code snippet to illustrate a covariant return type:The above code compiles successfully! Although the return types do not match, ReturnString.get() is a valid override of ReturnValue.get(). ReturnString.get() has a covariant return type.

Hope it helps!
Kind regards,
Roel
+Pie Number of slices to send: Send
 

Roel De Nijs wrote:Correct! And I would definitely not laugh about this one. 50.00 or 50.0 actually does matter: the 1st one has 4 significant figures, the latter one only 3.


I don't know about significant figures, thanks

Roel De Nijs wrote:Here you are wrong! To have a valid override the return type must not be the same. Since Java 5 covariant return types are supported.


I am confused according to the first sentence in this post:
“In this case, you can override a method a method by declaring a new method with the signature and return type as the method in the parent class”.
But it is written on page 248 obviously:

4. If the method returns a value, it must be the same or a subclass of the method in the parent class, known as covariant return types.


But I didn't understand "covariant return types" well when I read it. I decided to explore it but you have already written about it. Everthing is clear after examples, thanks a lot
+Pie Number of slices to send: Send
Logged the first two. I agree that "name" is redundant. We added it for emphasis.
1
+Pie Number of slices to send: Send
 

Mushfiq Mammadov wrote:But I didn't understand "covariant return types" well when I read it. I decided to explore it but you have already written about it. Everthing is clear after examples, thanks a lot


Just for completeness: it only works with objects, not with primitives. So this is not a valid override!You can assign an int to a long, but int is not a subtype (subclass) of long. So Y.get() is not a valid override of X.get() and you'll get a compiler error.

And don't get fooled by a trick question using the primitive wrapper classes, e.g.Although you can assign an int to a long, Integer IS-NOT-A Long! So Z.getNok() is not a valid override of W.getNok() and you'll get a compiler error. But Integer IS-A Number, so Z.getOk() is a valid override of W.getOk(). Hence those method names

Hope it helps!
Kind regards,
Roel
+Pie Number of slices to send: Send
 

Roel De Nijs wrote:
Just for completeness: it only works with objects, not with primitives. So this is not a valid override!You can assign an int to a long, but int is not a subtype (subclass) of long. So Y.get() is not a valid override of X.get() and you'll get a compiler error.

And don't get fooled by a trick question using the primitive wrapper classes, e.g.Although you can assign an int to a long, Integer IS-NOT-A Long! So Z.getNok() is not a valid override of W.getNok() and you'll get a compiler error. But Integer IS-A Number, so Z.getOk() is a valid override of W.getOk().



It is interesting and important information. I will practise using the primitive wrapper classes. Thanks a lot, Roel
Acetylsalicylic acid is aspirin. This could be handy too:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1308 times.
Similar Threads
“Overloading Methods” topic, chapter 4 (Java OCA 8 Programmer I Study Guide)
Some minor typos, chapter 3 (Java OCA 8 Programmer I Study Guide)
Some minor typos in chapter 4 (Java OCA 8 Programmer I Study Guide)
Some minor typos about String methods, on page 108,109,110 (Java OCA 8 Programmer I Study Guide)
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:50:55.