generics – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Mon, 04 Jul 2022 06:11:12 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg generics – Programmerbay https://programmerbay.com 32 32 Explain Wildcard Concept And Type of Wildcard in Java Generics https://programmerbay.com/explain-wildcard-concept-and-type-of-wildcard-in-java-generics/ https://programmerbay.com/explain-wildcard-concept-and-type-of-wildcard-in-java-generics/#respond Sun, 21 Mar 2021 15:47:21 +0000 https://programmerbay.com/?p=8321 In Java Generics, a Wildcard is represented by a ? or Question mark that resembles Unknown Type which is used to resolve incompatibility issue. Further, Wildcard can be used in various places such as, in form of the type parameter, field, as  return type and local variable. It can’t be used to instantiate a generic class or invoke a generic method.

In short, a wildcard concept provides a way to cast a class type to its superclasses or subclasses.

Situation:

public class WildCardIssue {
    public static void main(String[] args){
        ArrayList<String> arrayList = new ArrayList<String>();
        WildCardIssue obj = new WildCardIssue();
        obj.print(arrayList);
    }
    
    void print(ArrayList<String> obj){
        // Doing some Operations
        obj.add("Hi");
        obj.add("This is Programmerbay");
        obj.add("Wildcard");
        System.out.println(obj);

    }
    
}

Explanation:

The method is capable of handling ArrayList of String type only and can be invoked for String ArrayList. Further, operations within that method can be performed which is compatible with the respective passed parameter type. In this case, its support limits to String Object.

Suppose, if requirement changes, the method would no longer be useful. Instead, to overcome such situation, we can use wildcard argument or ? to provide support for other types also.

Wildcards can be classified into three types, Upper Bounded wildcard, Lower Bounded Wildcard, and Unbounded Wildcard.

Type of Wildcards in Java Generics :

Unbounded Wildcard:

An unbounded wildcard is specified with <?> which provides the capability to support for unknown types. It is suitable use when the behaviour of a method is not bounded to specific type and also not dependent on Type Parameter.

void print (ArrayList<?> obj) :  With the use of Unbounded Wildcard, a method becomes capable of accepting any unknown type and provides the advantage of code reusability and reduce the code size. The method can be invoked for any type as its param comes with Wildcard or ? argument , whether it is string,integer,double or any other.

The below program provides flexibility to support various type of object, but throw compile time error while performing add operation on a list.

public class WildCardIssue {
    public static void main(String[] args){
        ArrayList<String> arrayList = new ArrayList<String>();
        WildCardIssue obj = new WildCardIssue();
        obj.print(arrayList);

        ArrayList<Integer> integerArrayList = new ArrayList<Integer>();
        obj.print(integerArrayList);
    }

    void print(ArrayList<?> obj){
        obj.add("Hi");   // Compile time error,
        obj.add("This is Programmerbay");
        obj.add("Wildcard");
        System.out.println(obj);

    }

}

In case of List, only “null” can be added as null is member of any type. Therefore, wildcards are generally used for non-manipulation operations only.

The below example would work fine:

public class WildCardIssue {
    public static void main(String[] args){
        ArrayList<String> arrayList = new ArrayList<String>();
        WildCardIssue obj = new WildCardIssue();

        // Doing some operation
        arrayList.add("Hi");
        arrayList.add("This is Programmerbay");
        arrayList.add("Wildcard");
        obj.print(arrayList);

        ArrayList<Integer> integerArrayList = new ArrayList<Integer>();
        integerArrayList.add(5);
        integerArrayList.add(10);
        integerArrayList.add(20);
        obj.print(integerArrayList);
    }

    void print(ArrayList<?> obj){
        System.out.println(obj);

    }

}

Output:

[Hi, This is Programmerbay, Wildcard]
[5, 10, 20]

Upper Bounded Wildcard:

An Upper Bounded Wildcard is expressed by using <? extends anyClassName or anyInterfaceName>. It restricts the acceptable types to the extended className object or its subclass. For example, extending Number class would restrict the passed parameter type to Number Object or it subclasses such as Integer, Double and more.

void print (ArrayList<? extends anyClass or anyInterface> obj) : In Upper Bounded wildcard, the ? or question mark always extends a class or an interface. The method can be invoked when passed param type is either the passed class type or its child classes, in case of interface, either the interfaces or implementation classes. It also triggers compile time error while performing add operation in arraylist.

The below ‘print’ method supports only Number or it subclasses such as Double,Integer.

public class UpperBoundedWildCardExample {
    public static void main(String[] args){
        UpperBoundedWildCardExample obj = new UpperBoundedWildCardExample();

        ArrayList<Integer> integerArrayList = new ArrayList<Integer>();
        // Doing some operations

        integerArrayList.add(5);
        integerArrayList.add(10);
        integerArrayList.add(20);
        obj.print(integerArrayList);
    }

    void print(ArrayList<? extends Number> arrayList){
        for (Number singleElement:
             arrayList) {
            System.out.println(singleElement);
        }

    }

}

Lower Bounded Wildcard:

A Lower Bounded Wildcard is specified by using <? super anyClassName or anyInterfaceName>. It restricts the acceptable types to the passed className object or its superclasses (ancestors) only. For example, passing Integer would restrict the passed parameter type to Integer Object or its superclasses such as Number.

void print (ArrayList<? super anyClass or anyInterface> obj) : In Lower bounded wildcard, ? is used to set lower bound which is not allowed at class level with T type parameter. But, at method level Super keyword is allowed . The method can be invoked when passed param type is either class type or its parent classes.

The className type is allowed to perform add operation in Lower Bounded Wildcard

public class LowerBoundedExample {
    public static void main(String[] args) {
        LowerBoundedExample obj = new LowerBoundedExample();

        ArrayList<String> stringArrayList = new ArrayList<String>();
        obj.print(stringArrayList);
    }

    void print(ArrayList<? super String> arrayList) {
        arrayList.add("Hi");
        arrayList.add("String is allowed in Lower bound");
        arrayList.add(null);
        System.out.println(arrayList);

    }
}

 

As compared to previously discussed wildcards types, a lower bounded wildcard provides the flexibility to add class object passed in type argument to an ArrayList. For instance, <? super Integer> would allow adding elements of Integer type to an arraylist.

]]>
https://programmerbay.com/explain-wildcard-concept-and-type-of-wildcard-in-java-generics/feed/ 0
Bounded Type Parameter in Java Generics https://programmerbay.com/bounded-type-parameter-in-java-generics/ https://programmerbay.com/bounded-type-parameter-in-java-generics/#respond Sun, 21 Mar 2021 15:03:32 +0000 https://programmerbay.com/?p=8314 In Java Generics, a situation can arise where we need to restrict or bound an object type to specific types only in order to narrow down the acceptable types supported in the type parameter. For instance, a method that deals with numbers, should accept Number and its sub-classes.

Screenshot from 2021 03 09 17 53 26

Bounded Types:

The concept of limiting type parameter upto certain type is known as bounded types. In this, extends keyword is used to bound the type parameter.

It simply tells the compiler to allow only parent class or its subclasses types that T has extended, meaning,it restricts in passing types.

For example, if T extends Number, then only the type of Number class itself or its child classes such as integer,double and others are applicable.

Point to remember:

– In generics, one can extends an interface too.

– Super keyword terminology is not applied with Type Parameter

– extends keywords is used for bounded types

Syntax :

class A <T extends AnyClassName or InterfaceName> {​ // it can be class or interface

​
}​

Java program to demonstrate Bounded Type Parameter

public class BoundedTypeExample<T extends Number> {

void print(T num){
System.out.println("Only Number is allowed "+ num);
}

public static void main(String[] args) {
BoundedTypeExample<Integer> boundedTypeExample = new BoundedTypeExample();
boundedTypeExample.print(4);

}
}

However, passing string type would trigger compile time error.

public class BoundedTypeExample<T extends Number> {

void print(T num){
System.out.println("Only Number is allowed "+ num);
}

public static void main(String[] args) {
BoundedTypeExample<String> boundedTypeExample = new BoundedTypeExample(); // Type parameter 'java.lang.String' is not within its bound; should extend 'java.lang.Number'
boundedTypeExample.print("4");

}
}

Is it Possible to extend class and interface together in generics ?

When there is a need of having type that must be type bounded to a class and at the same time, it  should implement an interface. For such situation, there is a way to achieve this functionality.

Syntax:

class ClassName < T extends AnyClassName & AnyInterfaceName >{

}

The syntax simply means, an object of type T must extend the given class and implement the interface at the same time. This is another way to define bounded type.

It is important to remember that when using class and interface in combination, always class comes first and then after interfaces.
However, multiple classes cannot be extended since java doesn’t allow Multiple inheritance concept.

Java Program to demonstrate Bounded Type Parameter with combination of Class and Interface

public class BoundedTypeExample<T extends Number & Serializable> {

void print(T num){
System.out.println(num);
}

public static void main(String[] args) {
BoundedTypeExample<Integer> boundedTypeExample = new BoundedTypeExample<>();
boundedTypeExample.print(2);

}
}

Explanation: Above example, simply prints integer value, and integer is subclass of Number and also Implement Serializable interface.

]]>
https://programmerbay.com/bounded-type-parameter-in-java-generics/feed/ 0