• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generic question

 
zheng li
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why?

how could i return E?
 
Horia Constantin
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I can say from start that that code will never compile because you cannot have 2 returns like that (one after another). It will not compile (and probably say statement unreachable).

The question that arises... Didn't you check the error given by the compiler at compile time? That should have been pretty obvious.

Next time pay more attention to what the compiler has to say (questions like these are to be expected at the exam).

Cheers,
Horia
 
Allan Cheong
Ranch Hand
Posts: 71
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you can return E. E is a generic type. You can only return an object of type reference B.
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes E is a generic type. At runtime, it actually doesn't denotes anything, so you can't instantiate an object of type E. You'll have to create an instance of an actual type (class or interface) that is compatible with E. Since here E here is <E extends B>, so you can return an instance of B or a subclass of B...
 
zheng li
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Yes E is a generic type. At runtime, it actually doesn't denotes anything, so you can't instantiate an object of type E. You'll have to create an instance of an actual type (class or interface) that is compatible with E. Since here E here is <E extends B>, so you can return an instance of B or a subclass of B...

thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic