• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Generic programming

 
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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<?,?> > { ... }
...

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
A teeny tiny vulgar attempt to get you to buy our stuff
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic