• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Using multiple interfaces to define a generic type

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I was solving the mock test from Khalid Mughal and came across this question

Which type constraints, when inserted at (1), will allow the class to compile?
Select the four correct answers.
(a) N extends Object
(b) N extends Comparable<N>
(c) N extends Object & Comparable<N>
(d) N extends Number
(e) N extends Number & Comparable<N>
(f) N extends Comparable<N> & Number
(g) N extends Integer
(h) N extends Integer & Comparable<N>


Ans: (b),(c),(e) and (g).

I did not come across a combination generic type like this before and just wanted to know if there are any rules in how to define such generic types.

For example can we say <N> Number<N> & Comparable<N> instead of N extends Number & Comparable<N> ?

Please explain the specifics of this kind of implementation.

Thanks,
Kakani
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well it's just a case of trying. Look for what the method needs. This method needs an Object with the method compareTo thus we need an class that implements Comparable<N>. I'll try to explain by example:

A: N always extends Object but does not implement Comparable<N>
B: Correct
C: N extends Object is implicit but correct because it implements Comparable<N>
D: Incorrect because Number doesn't implement Comparable
E: Correct
F: Incorrect because in the generic definition the concrete classes must come first and then the interface. It would be correct is it was N extends Number & Comparable<N>
G: Correct because Integer implements Comparable
H: Incorrect because "java.lang.Comparable cannot be inherited with different arguments: <N> and <java.lang.Integer>". Basically it means that because Integer already defines Comparable you can't define it again.
 
L K Kakani
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if we are using more than one class/interface to describe a generic type the concrete class must come first and then the interface. I guess I need to go through generics once more. Thank you Wouter.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic