• Post Reply Bookmark Topic Watch Topic
  • New Topic

How does Type Inference (type parameter resolution) work?  RSS feed

Rahul Saple
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been reading this book by Khalid Mughal for my SCJP and it has this particular example which i fail to understand.
Basically, there are two subclasses each for two different superclasses and they are compared using generic methods. One of the superclasses implements the Comparable interface, while the other doesn't.

I edited the unneccesary code (as much as i could )
Here goes the code:

jProgCMP = max(jProgCMP, jProgCMP); // (4) Compile-time Error
progCMP = WhoIsComparable.<ProgrammerCMP>max(jProgCMP, jProgCMP);

Why does the compiler fail to infer that the parameter type is a ProgrammerCMP when one isnt mentioned and both the objects are of the same subclass

progCMP = max(jProgCMP, cProgCMP);//Compiles and Runs
And likewise, how does the compiler correctly infer the parameter in the absence of an explicit type parameter, when the objects are of different sublasses.

Does the type of the reference being assigned a value play a role?
What are the exact steps that the compiler takes to infer a type parameter?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!