Skip to content
Related Articles

Related Articles

Improve Article

Diamond operator for Anonymous Inner Class with Examples in Java

  • Last Updated : 16 Jan, 2020

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:






// 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);
    }
}

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 DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :