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

    Default What is the point of upper bounded type parameters in generics?

    In the following class, the generic type Bar has been instantiated with the type parameter Number. But I was able to pass an Integer to a method of Bar which gets an object of the type same as the type parameter of Bar, BECAUSE Integer enjoys an IS-A relationship with Number. That is, every Integer is a Number.

    Java Code:
    public class Bar<T> {	
    	private void aMethod(T t) {
    		System.out.println(t);;
    	}
    
    	public static void main(String[] args) {
    		
    		Bar<Number> bar = new Bar<>();
    		
    		Integer five = 5;
    		bar.aMethod(five);
    	}	
    }
    Then they are saying that

    times when you want to restrict the types that can be used as type arguments in a parameterized type. For example, a method that operates on numbers might only want to accept instances of Number or its subclasses. This is what bounded type parameters are for.
    This is an example of what I understand they are saying:

    Java Code:
    public class Foo<T extends Number> {	
    	private void aMethod(T t) {
    		System.out.println(t);
    	}
    
    	public static void main(String[] args) {
    		
    		Foo<Integer> foo = new Foo<>();
    		
    		Integer five = 5;
    		foo.aMethod(five);
    	}
    }
    But i could do that same thing in the above version without using any bounded type parameters. So what is the point of upper bounds then? Why do they exist?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: What is the point of upper bounded type parameters in generics?

    Primitives and objects are covariant with their array types.
    That just means that if S is a subtype of T, then S[] is also a subtype of T[].

    Not so with generics.

    Integer is a subtype of Number but List<Integer> is not a subtype of List<Number>.
    So you can't do the following:

    Java Code:
    List<Integer> n = new ArrayList<>();
    List<Number> a =  n; // compiler error.
    But you can do the following;

    Java Code:
    List<? extends Number> a = n;
    Because List<Integer> is a subtype of List<? extends Number>
    and you can always assign a subtype instance to its supertype.

    So if you had two separate lists, one of Integer and one of Double
    and you wanted to pass them to the same method, it could be declared
    like one of the following:

    Java Code:
    public static <T extends Number> void foo(List<T> list) {
    }
    
    public void foo(List<? extends Number> list) {
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: What is the point of upper bounded type parameters in generics?

    Also, try sticking the following code in your first Bar<T>, and then try it with Bar<T extends Number>:
    Java Code:
    public long doubleIt(T t) {
        return t.longValue() * 2;
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: What is the point of upper bounded type parameters in generics?

    @Jim829 Thank you =)

  5. #5
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: What is the point of upper bounded type parameters in generics?

    Quote Originally Posted by Tolls View Post
    Also, try sticking the following code in your first Bar<T>, and then try it with Bar<T extends Number>:
    Java Code:
    public long doubleIt(T t) {
        return t.longValue() * 2;
    }
    It doesn't work, because the compiler only lets us invoke methods of the declared type (which is Number, and which does Not have the longValue() method) right?

    And, it works with the generic version, which means that when using a type parameter bound by an upper bound, we can invoke any method of the upper bound or any of its subclasses.

    Thank you.

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

    Default Re: What is the point of upper bounded type parameters in generics?

    Yep.
    The base <T> will only treat T as an Object.
    <T extends Number> will treat it as a Number and so knows that longValue() is allowed.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: What is the point of upper bounded type parameters in generics?

    @Tolls right, thank you. =)

Similar Threads

  1. The point of existence of Generics
    By Zarah in forum New To Java
    Replies: 9
    Last Post: 05-11-2016, 05:38 PM
  2. Bounded Type Parameters
    By allaudin in forum New To Java
    Replies: 11
    Last Post: 08-05-2013, 12:58 PM
  3. Bounded WildCard as Type Parameter
    By jim829 in forum Advanced Java
    Replies: 0
    Last Post: 07-29-2013, 01:53 AM
  4. Replies: 2
    Last Post: 01-24-2013, 10:57 AM

Posting Permissions

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