Results 1 to 4 of 4

Thread: Generics

  1. #1
    sakshamkum is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Post Generics

    there is a class as

    class Data<T>
    {
    public String getClassName()
    {
    return this.getClass().toString();
    }
    // and more functions
    }

    and Integer and Double subclasses of Number
    so a function
    public void process(Number n)
    {
    // the definition
    }
    can accept
    process(new Integer(100));
    and
    process(new Double(100.0));

    why cannot we use
    new Data<Integer>() or new Data<Double>()
    in a function similar to
    public void process(Data<Number> n)
    {
    // the definition
    }

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    try

    public<T extends Number> void process(Data<T> n)
    {
    // the definition
    }

  3. #3
    sakshamkum is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Post clearifying my question

    the question I wanted to ask is conceptual. the question is that why does java does not allow the generic type of two classes extending a common class [example Integer and Double extend Number] to be used where the generic type of base class is required.
    why are the following calls of the latter function process() are not supported
    process(new Data<Integer>());
    process(new Data<Double>());
    where function process is defined as
    public void process(Data<Number> n)
    {
    //the definition
    }

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

Similar Threads

  1. Generics
    By bschmitt78 in forum Advanced Java
    Replies: 3
    Last Post: 03-16-2010, 02:21 AM
  2. generics
    By tascoa in forum Forum Lobby
    Replies: 2
    Last Post: 10-09-2008, 07:58 PM
  3. Help w/ generics
    By Hollywood in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 03:08 AM
  4. Generics
    By sireesha in forum New To Java
    Replies: 2
    Last Post: 01-10-2008, 11:08 PM
  5. Generics
    By eva in forum New To Java
    Replies: 2
    Last Post: 01-04-2008, 09:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •