• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generic programming  RSS feed

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"A reference type that is used to further describe a type parameter. It restricts the set of types that can be used as type arguments and gives access to the non-static methods that it defines.
A type parameter can be unbounded. In this case any reference type can be used as type argument to replace the unbounded type parameter in an instantiation of a generic type.

Alternatively can have one or several bounds. In this case the type argument that replaces the bounded type parameter in an instantiation of a generic type must be a subtype of all bounds.

The syntax for specification of type parameter bounds is:

<TypeParameter extends Class & Interface 1 & ... & Interface N >

A list of bounds consists of one class and/or several interfaces.

Example (of type parameters with several bounds):

class Pair<A extends Comparable<A> & Cloneable ,
B extends Comparable<B> & Cloneable >
implements Comparable<Pair<A,B>>, Cloneable { ... }

This is a generic class with two type arguments A and B , both of which have two bounds."

Sourced from here.

1. I have not yet come across a bitmask for extending interfaces, is this fine? Would the programmer need to implement Comparable.compareTo() within the generic class?
2. How does the programmer indicate bounds that limit types that can be instantiated for a generic type, something like the following?

...
class X0 <T extends int > { ... } // error
class X1 <T extends Object[] > { ... } // error
class X2 <T extends Number > { ... }
class X3 <T extends String > { ... }
class X4 <T extends Runnable > { ... }
class X5 <T extends Thread.State > { ... }
class X6 <T extends List > { ... }
class X7 <T extends List<String> > { ... }
class X8 <T extends List<? extends Number> > { ... }
class X9 <T extends Comparable<? super Number> > { ... }
class X10<T extends Map.Entry<?,?> > { ... }
...

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Camilleri wrote:1. I have not yet come across a bitmask for extending interfaces, is this fine? Would the programmer need to implement Comparable.compareTo() within the generic class?

This does not have anything to do with bitmasks. The "&" sign is used if you want to specify multiple bounds. It happens to be the same character as is used for the bitwise AND operation, but it does not have anything to do with it.

If you'd have a type parameter with a bound, for example <T extends Comparable<T>> then it means that for T you can only use types that extend Comparable<T>. If you would try to use a different concrete type for T then you would get a compilation error.

Jon Camilleri wrote:2. How does the programmer indicate bounds that limit types that can be instantiated for a generic type, something like the following?

Yes and ofcourse you cannot write extends int or extends Object[], because primitive types such as int and arrays are not classes that you can extend.

These are all quite advanced generic programming concepts.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!