Bounded types with generics in Java

Bounded Type Parameters

There may be 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.

  • Sometimes we don’t want whole class to be parameterized, in that case we can create java generics method. Since constructor is a special kind of method, we can use generics type in constructors too.
  • Suppose we want to restrict the type of objects that can be used in the parameterized type. For example in a method that compares two objects and we want to make sure that the accepted objects are Comparables.
  • The invocation of these methods is similar to unbounded method except that if we will try to use any class that is not Comparable, it will throw compile time error.

Declare a bounded type parameter



  1. List the type parameter’s name.
  2. Along by the extends keyword
  3. And by its upper bound.(which in below example is A.)
  4. Syntax

    <T extends superClassName>

    Note that, in this context, extends is used in a general sense to mean either “extends” (as in classes). Also, This specifies that T can only be replaced by superClassName, or subclasses of superClassName. Thus, superclass defines an inclusive, upper limit.

    Let’s take an example on how to implement bounded types (extend superclass) with generics.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // This class only accepts type parametes as any class
    // which extends class A or class A itself.
    // Passing any other type will cause compiler time error
       
    class Bound<T extends A>
    {
           
        private T objRef;
           
        public Bound(T obj){
            this.objRef = obj;
        }
           
        public void doRunTest(){
            this.objRef.displayClass();
        }
    }
       
    class A
    {
        public void displayClass()
        {
            System.out.println("Inside super class A");
        }
    }
       
    class B extends A
    {
        public void displayClass()
        {
            System.out.println("Inside sub class B");
        }
    }
       
    class C extends A
    {
        public void displayClass()
        {
            System.out.println("Inside sub class C");
        }
    }
      
    public class BoundedClass
    {
        public static void main(String a[])
        {
              
            // Creating object of sub class C and 
            // passing it to Bound as a type parameter.
            Bound<C> bec = new Bound<C>(new C());
            bec.doRunTest();
              
            // Creating object of sub class B and 
            // passing it to Bound as a type parameter.
            Bound<B> beb = new Bound<B>(new B());
            beb.doRunTest();
              
            // similarly passing super class A
            Bound<A> bea = new Bound<A>(new A());
            bea.doRunTest();
              
        }
    }

    chevron_right

    
    

    Output :

    Inside sub class C
    Inside sub class B
    Inside super class A
    

    Now, we restricted to only of type A and its sub classes, So it will throw an error for any other type or sub classes.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // This class only accepts type parametes as any class
    // which extends class A or class A itself.
    // Passing any other type will cause compiler time error
       
    class Bound<T extends A>
    {
           
        private T objRef;
           
        public Bound(T obj){
            this.objRef = obj;
        }
           
        public void doRunTest(){
            this.objRef.displayClass();
        }
    }
       
    class A
    {
        public void displayClass()
        {
            System.out.println("Inside super class A");
        }
    }
       
    class B extends A
    {
        public void displayClass()
        {
            System.out.println("Inside sub class B");
        }
    }
       
    class C extends A
    {
        public void displayClass()
        {
            System.out.println("Inside sub class C");
        }
    }
      
    public class BoundedClass
    {
        public static void main(String a[])
        {
            // Creating object of sub class C and 
            // passing it to Bound as a type parameter.
            Bound<C> bec = new Bound<C>(new C());
            bec.doRunTest();
      
            // Creating object of sub class B and 
            // passing it to Bound as a type parameter.
            Bound<B> beb = new Bound<B>(new B());
            beb.doRunTest();
      
            // similarly passing super class A
            Bound<A> bea = new Bound<A>(new A());
            bea.doRunTest();
              
            Bound<String> bes = new Bound<String>(new String());
            bea.doRunTest();
        }
    }

    chevron_right

    
    

    Output :

    error: type argument String is not within bounds of type-variable T
    

    Multiple Bounds

    Bounded type parameters can be used with methods as well as classes and interfaces.

    Java Generics supports multiple bounds also, i.e . In this case A can be an interface or class. If A is class then B and C should be interfaces. We can’t have more than one class in multiple bounds.

    Syntax


    <T extends superClassName & Interface>
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    class Bound<T extends A & B>
    {
          
        private T objRef;
          
        public Bound(T obj){
            this.objRef = obj;
        }
          
        public void doRunTest(){
            this.objRef.displayClass();
        }
    }
      
    interface B
    {
        public void displayClass();
    }
      
    class A implements B
    {
        public void displayClass()
        {
            System.out.println("Inside super class A");
        }
    }
      
    public class BoundedClass
    {
        public static void main(String a[])
        {
            //Creating object of sub class A and 
            //passing it to Bound as a type parameter.
            Bound<A> bea = new Bound<A>(new A());
            bea.doRunTest();
              
        }
    }

    chevron_right

    
    

    Output :

    Inside super class A
    

    Reference :

    https://docs.oracle.com/javase/tutorial/java/generics/types.html

    This article is contributed by Saket Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



    My Personal Notes arrow_drop_up


    Article Tags :
    Practice Tags :


    16


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.