0

While working on a project, I came across the following code segment which appears to provide code, entirely contained inside a new variable declaration, which appears to override a method. I've, come across code of this form before but admittedly, I do not fully understand it. If anyone could explain the programming mechanisms upon which this code is based, I'd be very truly grateful. Particularly, when are overridden methods of this sort permitted inside of variable declarations. What other sorts of data structures allow such behavior? When is it advantageous to write code of such nature? Why not override the method outside of a variable declaration?

tempRequests.sort(new Comparator<Integer>() 
{           
    @Override

    public int compare(Integer integer1, Integer integer2) 
    {           
        return integer1.compareTo(integer2);        
    }


});
Trixie the Cat
  • 317
  • 3
  • 18
  • 2
    that's called "anonymous class" declaration. instead of creating a separate class, you create it inside of method argument area. For simplicity and compactness I guess, but sometimes it degrades readability, especially if the number of overridden methods is large – mangusta Aug 02 '19 at 23:52
  • 1
    Possible duplicate of [Polymorphism vs Overriding vs Overloading](https://stackoverflow.com/questions/154577/polymorphism-vs-overriding-vs-overloading) – bashedandzshed Aug 02 '19 at 23:52
  • 1
    Comparator is an interface. When you try to instantiate an Interface/Abstract class you should also implement the method defined by the abstraction. In this case the method compare(). – VeryNiceArgumentException Aug 03 '19 at 00:01
  • 1
    FYI, this is equivalent to `tempRequests.sort(null);` – shmosel Aug 03 '19 at 00:10
  • 1
    It is anonymous class. In this example it was used to override method of Comparator class. Anonymous classes are used instead of creating children classes and typically need usage only one time – bakero98 Aug 03 '19 at 00:24
  • And note that lambdas replace most situations where explicit anonymous classes used to be needed. – chrylis -cautiouslyoptimistic- Aug 03 '19 at 02:13

2 Answers2

1

What other sorts of data structures allow such behavior?

-> You can sort objects by implements interface Comparable. For example:

 public class Car implements Comparable<Car> {
     private String name;
     @Override
     public int compareTo(Car b) {
     return name.compareTo(b.name);
      }
    }

->You can also use Comparator without override method compare inside the inner class.

public class Car implements Comparator<Car> {
     private String name;
     private double price;
     @Override
     public int compare(Car b1, Car b2) {
     return b1.price - b2.price;
      }
    }

When is it advantageous to write code of such nature? Why not override the method outside of a variable declaration?

-> Image that after use sort object Car by name, you want to sort by something else (like by price, by weight).How to do this when you want to sort objects in different ways at different times? We use Comparator with define inside the inner class to do this.

*Additionally, Comparator is a functional interface since an only abstract method to implement. You can rewrite using a funky syntax in one line of code: Ex:

Compareator<Car> byPrice = (b1,b2) -> b1.price - b2.price;
hoapham
  • 180
  • 5
  • 10
1

This mechanism has been explained well in the comments. As an aside: ever since Java 8, this usage of anonymous classes is considered somewhat old fashioned, as it can be replaced with a simple Lambda expression:

tempRequests.sort((l, r) -> l.compareTo(r));        

This applies to all "Functional Interfaces", which is defined as an interface with exactly one non-static and non-default method.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • “Old fashioned”? I must disagree. How else does one write, for example, a Swing mouse listener? – VGR Aug 03 '19 at 02:35
  • I'd say Swing itself is pretty old fashioned. Are people still using it? – Sean Patrick Floyd Aug 06 '19 at 13:05
  • While there aren’t a lot of Java desktop applications in general, nearly all of the ones I’ve seen are Swing. What is old fashioned about it? – VGR Aug 06 '19 at 14:28