This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Generics" Watch "Doubt in Generics" New topic
Author

Doubt in Generics

Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121


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.

OCPJP 6 - 96%
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18503
    
  40


Please QuoteYourSources


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 721
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

Joined: Sep 16, 2013
Posts: 121

Henry Wong wrote:
Please QuoteYourSources


It's from K and B question book for SCJP 6
Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121

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

Joined: Sep 16, 2013
Posts: 121

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
Sheriff

Joined: Sep 28, 2004
Posts: 18503
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18503
    
  40

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

Joined: Sep 16, 2013
Posts: 121

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
Sheriff

Joined: Sep 28, 2004
Posts: 18503
    
  40

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

Joined: Jul 29, 2012
Posts: 721


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

Joined: Sep 16, 2013
Posts: 121

Thanks guys. I got it
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 721
Also, try this something like this to see if it compiles:
timo corn
Ranch Hand

Joined: Oct 26, 2013
Posts: 68

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?


hth
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Generics
 
Similar Threads
Generic upper bounded wildcard instantiation known at run time
Generics and use of wildcard
Problem in Generics
generic type instantiation question