Diamond operator for Anonymous Inner Class with Examples in Java

Prerequisite: Anonymous Inner Class

Diamond Operator: Diamond operator was introduced in Java 7 as a new feature.The main purpose of the diamond operator is to simplify the use of generics when creating an object. It avoids unchecked warnings in a program and makes the program more readable. The diamond operator could not be used with Anonymous inner classes in JDK 7. In JDK 9, it can be used with the anonymous class as well to simplify code and improves readability. Before JDK 7, we have to create an object with Generic type on both side of the expression like:

// Here we mentioned the generic type
// on both side of expression while creating object
List<String> geeks = new ArrayList<String>();

When Diamond operator was introduced in Java 7, we can create the object without mentioning generic type on right side of expression like:

List<String> geeks = new ArrayList<>();

Problem with Diamond Operator in JDK 7?

With the help of Diamond operator, we can create an object without mentioning the generic type on the right hand side of the expression. But the problem is it will only work with normal classes. Suppose you want to use the diamond operator for anonymous inner class then compiler will throw error message like below:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Program to illustrate the problem
// while linking diamond operator
// with an anonymous inner class
  
abstract class Geeksforgeeks<T> {
    abstract T add(T num1, T num2);
}
  
public class Geeks {
    public static void main(String[] args)
    {
        Geeksforgeeks<Integer> obj = new Geeksforgeeks<>() {
            Integer add(Integer n1, Integer n2)
            {
                return (n1 + n2);
            }
        };
        Integer result = obj.add(10, 20);
        System.out.println("Addition of two numbers: " + result);
    }
}

chevron_right


Output:

prog.java:9: error: cannot infer type arguments for Geeksforgeeks
        Geeksforgeeks  obj = new Geeksforgeeks  () {
                                                        ^
  reason: cannot use '' with anonymous inner classes
  where T is a type-variable:
    T extends Object declared in class Geeksforgeeks
1 error

Java developer extended the feature of the diamond operator in JDK 9 by allowing the diamond operator to be used with anonymous inner classes too. If we run the above code with JDK 9, then code will run fine and we will generate the below output.

Output:

Addition of two numbers: 30

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up


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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.