Which, taken independently, are true? (Choose all that apply.)
A. If line 5 is uncommented, compilation fails due to an error at line 7.
B. If line 5 is uncommented, compilation fails due to an error at line 8.
C. If line 5 is uncommented, compilation fails due to an error at line 9.
D. If line 6 is uncommented, compilation fails due to an error at line 7.
E. If line 6 is uncommented, compilation fails due to an error at line 8.
F. If line 6 is uncommented, compilation fails due to an error at line 9.
Answer (for Objective 6.4):
A, B, C, and D are correct. The generic type of the reference <? extends Organic>
says that the generic type of the instantiation can be either Organic, or a subtype of
Organic. Since the compiler doesn’t know this instantiation generic type (runtime type),
it does NOT bind any value to its generic criteria, so A, B, and C are correct. On the other
hand, the generic type of the reference <? super Aliphatic> says that the generic type
of the instantiation can be either Aliphatic, or a supertype of Aliphatic. Although
the compiler doesn’t know the instantiation generic type, it knows that it will be either
Aliphatic, or a supertype of Aliphatic—such types can bind any value that is either
Aliphatic or a subtype of it. Therefore, D is correct.
E and F are incorrect based on the above.
OCPJP 6 - 96%
Currently working on Scala at Knoldus Software
Himai Minh wrote:This question was asked before at:
https://coderanch.com/t/607980/java-programmer-SCJP/certification/generic-type-instantiation
Please read this link and see if you are clear.
OCPJP 6 - 96%
Currently working on Scala at Knoldus Software
OCPJP 6 - 96%
Currently working on Scala at Knoldus Software
Sidharth Khattri wrote:
If Line 6 is uncommented:
6. //Organic<? super Aliphatic> compound = new Aliphatic<Organic>();
<? super Aliphatic> can only be <Organic> or <Aliphatic>
So,
<Organic> can accept <Organic> or <Aliphatic> or <Hexane>
<Aliphatic> can accept <Alpihatic> or <Hexane>
Since both <Organic> and <Aliphatic> can accept <Aliphatic> and <Hexane>, so <Organic> i.e line 7 is ruled out, hence answer D.
Sidharth Khattri wrote:
Similarly, if line 5 is uncommented:
5. //Organic<? extends Organic> compound = new Aliphatic<Organic>();
<? extends Organic> can be either <Organic> or <Aliphatic> or <Hexane>
So,
<Organic> can accept <Organic> or <Aliphatic> or <Hexane>
<Aliphatic> can accept <Aliphatic> or <Hexane>
<Hexane> can accept <Hexane>
Since all of the following: <Organic> and <Aliphatic> and <Hexane> can accept <Hexane>, then why is line 9 ruled out for answer C?
Then why is C included in the answer? I don't understand the logic.
Henry Wong wrote:
Sidharth Khattri wrote:
Similarly, if line 5 is uncommented:
5. //Organic<? extends Organic> compound = new Aliphatic<Organic>();
<? extends Organic> can be either <Organic> or <Aliphatic> or <Hexane>
So,
<Organic> can accept <Organic> or <Aliphatic> or <Hexane>
<Aliphatic> can accept <Aliphatic> or <Hexane>
<Hexane> can accept <Hexane>
Since all of the following: <Organic> and <Aliphatic> and <Hexane> can accept <Hexane>, then why is line 9 ruled out for answer C?
Then why is C included in the answer? I don't understand the logic.
It can also be of generic type <Propane>, <Butane>, and/or any of countless other classes that will extend Organic in the future (or in other software libraries) that this compile execution doesn't have access to. Do you think that it will be possible to have a class that subclass the Organic class, but doesn't subclass the Hexane class? ..... hint: yes
Henry
OCPJP 6 - 96%
Currently working on Scala at Knoldus Software
Sidharth Khattri wrote:
Yes it is possible.
Suppose Mundane were to extend Organic class, then,
<Mundane> could NOT accept <Organic> or <Aliphatic> or <Hexane>
in that case <Hexane> could be ruled out.
But isn't this out of the scope of the question? The question doesn't mentions any such scenario. It only have Organic, Aliphatic and Hexane in it.
OCPJP 6 - 96%
Currently working on Scala at Knoldus Software
hth
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |