Forums Register Login

Doubt in Generics

+Pie Number of slices to send: Send

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.



Can anyone please explain the answers to this question? I don't understand the explanation given for this question.
+Pie Number of slices to send: Send
1
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

Henry Wong wrote:
Please QuoteYourSources



It's from K and B question book for SCJP 6
+Pie Number of slices to send: Send
 

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.



But if line 5 is uncommented, Hexane could be passed to every references on which react is invoked, so line 9. should not cause any error?
+Pie Number of slices to send: Send
From what I understand from this link:
https://coderanch.com/t/607980/java-programmer-SCJP/certification/generic-type-instantiation

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.

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.
+Pie Number of slices to send: Send
 

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.



It can also be of generic type <Object>. Remember that Organic subclasses the Object class.

Henry
1
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
 

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



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.
+Pie Number of slices to send: Send
 

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.




The scope of the question is what the compiler will do -- and the compiler doesn't behave differently based on scenario.


It is simply an operation on a wildcard generic reference. It doesn't know the exact generic type, what other classes you are using. etc.

Henry
1
+Pie Number of slices to send: Send


It is all about what type of object react will accept as its parameter.

Consider these :


and suppose you have this:

You pass in a Hexane to react method . But compound's react method expects an Organic object as parameter.
Can this compile ?


The compiler won't let you pass a Hexane or any other type to react because the compile does not want the programmer to create a wrong assignment like this :

+Pie Number of slices to send: Send
Thanks guys. I got it
+Pie Number of slices to send: Send
Also, try this something like this to see if it compiles:
+Pie Number of slices to send: Send
Himai, your first post explained it very nice, and I tried the task in the second post:
If uncommenting

it compiles.
When uncommenting

compiler error when using react for the first time:The method react(Aliphatic) in the type Organic<Aliphatic> is not applicable for the arguments (Organic)

So only Organic works as you stated already in your first post?
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1044 times.
Similar Threads
Problem in Generics
Generic upper bounded wildcard instantiation known at run time
Generics and use of wildcard
Generic instantiation type
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 09:25:11.