Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Generics

 
Sidharth Khattri
Ranch Hand
Posts: 125
Java Linux Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
Himai Minh
Ranch Hand
Posts: 1310
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question was asked before at:
http://www.coderanch.com/t/607980/java-programmer-SCJP/certification/generic-type-instantiation

Please read this link and see if you are clear.
 
Sidharth Khattri
Ranch Hand
Posts: 125
Java Linux Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Please QuoteYourSources


It's from K and B question book for SCJP 6
 
Sidharth Khattri
Ranch Hand
Posts: 125
Java Linux Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himai Minh wrote:This question was asked before at:
http://www.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?
 
Sidharth Khattri
Ranch Hand
Posts: 125
Java Linux Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I understand from this link:
http://www.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.
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sidharth Khattri
Ranch Hand
Posts: 125
Java Linux Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Himai Minh
Ranch Hand
Posts: 1310
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 :

 
Sidharth Khattri
Ranch Hand
Posts: 125
Java Linux Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. I got it
 
Himai Minh
Ranch Hand
Posts: 1310
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, try this something like this to see if it compiles:
 
timo corn
Ranch Hand
Posts: 68
MyEclipse IDE PHP Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic