Bounded Type Parameters 49 What is a bounded Type Parameter? - - PowerPoint PPT Presentation

bounded type parameters
SMART_READER_LITE
LIVE PREVIEW

Bounded Type Parameters 49 What is a bounded Type Parameter? - - PowerPoint PPT Presentation

Bounded Type Parameters 49 What is a bounded Type Parameter? Restrict the types that may be used as type arguments for a parameterized type E.g. a generic methods will work only with Integer-like types 50 Using bounded types with


slide-1
SLIDE 1

Bounded Type Parameters

49

slide-2
SLIDE 2

What is a bounded Type Parameter?

  • Restrict the types that may be used as type arguments for a

parameterized type

  • E.g. a generic methods will work only with Integer-like types

50

slide-3
SLIDE 3

51

Using bounded types with Generic Methods

public class Box<T> { private T t; public void set(T t) {this.t = t;} public T get() {return t;} public <U extends Number> void inspect(U u){ System.out.println("T: " + t.getClass().getName()); System.out.println("U: " + u.getClass().getName()); } } Please Note This means “subtype” not inheritance

slide-4
SLIDE 4

Let’s check that Java does its job

52

Box.java:21: <U>inspect(U) in Box<java.lang.Integer> cannot be applied to (java.lang.String) integerBox.inspect("10"); ^ 1 error public static void main(String[] args) { Box<Integer> integerBox = new Box<Integer>(); integerBox.set(new Integer(10)); integerBox.inspect("some text"); // error: this actual parameter is a String! }

slide-5
SLIDE 5

Extra Free Feature! Bounded Types Parameters also allow to invoke methods defined in the bounds

53

public class NaturalNumber<T extends Integer> { private T n; public NaturalNumber(T n) { this.n = n; } public boolean isEven() { return n.intValue() % 2 == 0; } // ... } isEven(…) able to call intValue(…) Since n is

  • f data type T

n is of data type T means…

  • T happens to extend Integer
  • thus T features intValue() method
slide-6
SLIDE 6

New Requirement! What about introducing Multiple Bounds?

Definition

<T extends B1 & B2 & B3>

54

Note: This will be referred to as the leftmost bounded type If one of the bounds is a class, it must be specified first A type variable with multiple bounds is a subtype of all the types listed in the bound

slide-7
SLIDE 7

Example featuring a class

55

For example:

class A { /* ... */ } interface B { /* ... */ } interface C { /* ... */ } class D <T extends A & B & C> { /* ... */ }

If bound A is not specified first, you get a compile-time error:

class D <T extends B & A & C> { /* ... */ }

slide-8
SLIDE 8

public static <T> int countGreaterThan(T[] anArray, T elem) { // Return the # of elements greater than elem int count = 0; for (T e : anArray) if (e > elem) ++count; return count; }

Debug this Example

56

?

What do you think is the problem here?

!

The > operator works only w/ primitive data types… … not objects

// compiler error

slide-9
SLIDE 9

Debug this Example

57

!

T must extend the Comparable <T> interface

public static <T> int countGreaterThan(T[] anArray, T elem) { // Return the # of elements greater than elem int count = 0; for (T e : anArray) if (e > elem) ++count; return count; }

?

How do we fix this problem?

// compiler error

slide-10
SLIDE 10

Now we may rewrite our original attempt, using this new Comparable<T> interface

58

public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem){ int count = 0; for (T e : anArray) if (e.compareTo(elem) > 0) ++count; return count; } public interface Comparable<T> { public int compareTo(T o); } NOTE We use “extends” not “implements” Remember… This is about subtyping not inheritance or interface implementations