Results 1 to 4 of 4
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Defining Generic Types with actual Java type names, ratherthan generic type variables

    QUESTION:

    Android's `AsyncTask` class is a generic type, and we often extend it in our code.

    The question is that why do we have to write the actual Java types as the type-parameters/type-variables, rather than generic type-variables like T, U etc., in the definition of our subtype of `AsyncTask`, like

    Java Code:
    class MyTask extends AsyncTask<String, Integer, Long> {
    
      @Override
      protected Long doInBackground(String... paramsPassedToTask) {...}
    
      @Override
      protected void onProgressUpdate(Integer... progressIndicator) {...}
    
      @Override
      protected void onPostExecute(Long... long) {...}
    
    }
    But if I try to write something like

    Java Code:
    class MyTask extends AsyncTask<T1, T2, T3> {		
    ...		
    }
    I get 'T1 cannot be resolved to a type', 'T2 cannot be resolved to a type', 'T3 cannot be resolved to a type' compiler errors all over the place.

    BACKGROUND:

    I have been trying to learn about Generics from this tutorial and the idea I got is that you define a generic type with type-paramters/type-variables, which are not actual Java types but generic types usually denoted by capital letters

    Java Code:
    public class Box<T> {
    
    private T t;
    public void set(T t) { this.t = t; }
    public T get() { return t; }
    }
    Due to the use of generic type-parameters, the methods of this type are able to receive (where T is parameter type) and return (where T is return type) any type.

    Then, when we instantiate this type, we use the actual Java types.

    Java Code:
    Box<Integer> box = new Box<Integer>();
    Then when we call a method on this instance and pass an argument to a method (where T is used in method definition) or assign some value returned from a method (where the T is used in method definition) to a variable, the compiler checks if we are passing as argument (or assigning returned value to) a correct type.

    This lets the compiler ensure type safety at compile time.

    So yeah, we define generic types using generic-type-variables and instantiate them with names of actual Java reference types.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Defining Generic Types with actual Java type names, ratherthan generic type varia

    Can you respond to the posts on this thread? http://www.java-forums.org/new-java/...-contains.html
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Defining Generic Types with actual Java type names, ratherthan generic type varia

    I can't actually make out a question in that lot.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Defining Generic Types with actual Java type names, ratherthan generic type varia

    If you extend from a generic class, say Clazz<T> but your class isn't a generic class, you have to supply a real Java class, say 'class MyClass extends Clazz<String>'; if however your class is also a generic class, you can do this: class MyClass<T> extends Clazz<T>'.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Designing generic data type
    By anjibman in forum New To Java
    Replies: 0
    Last Post: 04-08-2013, 08:56 PM
  2. Generic Type Question
    By Firen4000 in forum Advanced Java
    Replies: 3
    Last Post: 07-12-2012, 11:31 PM
  3. Creating an array of generic type
    By colerelm in forum New To Java
    Replies: 1
    Last Post: 10-19-2011, 03:58 AM
  4. Type wildcards in Generic
    By gvm in forum New To Java
    Replies: 1
    Last Post: 10-22-2010, 04:39 PM
  5. Generic collection type
    By dreuzel in forum Advanced Java
    Replies: 7
    Last Post: 12-23-2009, 06:05 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
  •