1

Since java interfaces cannot be instantiated, how could interfaces be class members and be instantiate by class constructors etc.

jdk code:

public class PriorityQueue<E> extends AbstractQueue<E>
    implements java.io.Serializable {
 private final Comparator<? super E> comparator;
...
}

and user code:

PriorityQueue<Point> pq = new PriorityQueue<Point>(1005,new Comparator<Point>(){
            public int compare(Point p1,Point p2){
                ...
            }
        });

The above usage of PriorityQueue is valid (just for example). And the Comparator in the class PriorityQueue is actually an interface. So the interface become a member of class?! Besides, when instantiate the PriorityQueue, the interface is instantiated, which is so mysterious.

Cipher
  • 21
  • 3
  • It's the same as implementing the `Comparator` interface and instantiate the implementation and pass to the `PriorityQueue` constructor. It is just an anonymous class. – Mansur Jul 04 '19 at 12:04

3 Answers3

4

The syntax new Comparator<Point>(){...} creates an instance of an anonymous class that implements the Comparator<Point> interface. Therefore you are creating an instance of a class.

You can never create an instance of an interface, only an instance of a class that implements that interface.

Finally, an instance of a class that implements an interface can be assigned to a variable of that interface type. Hence private final Comparator<? super E> comparator can be assigned an instance of a class that implements the Comparator interface.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

If you declare a class member to be of the type of an interface, any class that implements that interface can be assigned to that member. This is useful if you don't really care about the implementation, as long as the methods defined in the interface are available.

As an example, List is an interface. I can assign an ArrayList or a LinkedList (or any other implementation) to a variable of type List, but I can only call the methods defined in the interface, and not methods specific to any of the implementing classes.

The code you give where a Comparator is instantiated actually creates an anonymous class that implements that interface. The method that follows is the way the interface is implemented.

Rick
  • 935
  • 2
  • 7
  • 22
0

"interfaces cannot be instantiated" is a rather blanket slogan that hides a more nuanced reality, and that should not really be remembered beyond first month of learning the language.

The code

new Comparator<Point>(){
            public int compare(Point p1,Point p2){
                ...
            }
        }

does two things :

compile time : creates an anonymous class that implements the interface (by providing a concrete implementation for the interface's methods) - and that class will also have the usual no-arg constructor so instances of that class can be created at run time.

run time : creates a run-time instance of that anonymous class by making sure the constructor is invoked.

So "interfaces cannot be instantiated" intends to explain that one cannot write

new List<X>();

(i.e. without providing an implementation for the interface methods), but that does not mean that it is impossible to create objects that are instances of classes that implement the interface, i.e. to create objects that are known and guaranteed to conform to the interface.

The question (and how it's worded) "how could interfaces be class members" also betrays some of your confusion here : the code

private final Comparator<? super E> comparator;

does not have for consequence that 'Comparator<...>' gets declared as a member of the class, instead it has for consequence that 'comparator' gets declared as a member of the class, and that member happens to be of type 'Comparator<...>' (which in this case means it must be of a type (/class) that is known to implement this interface).

Erwin Smout
  • 18,113
  • 4
  • 33
  • 52
  • Brilliant~! Such concise and detailed answer perfectly get rid of all my confusion. GREAT THX!! – Cipher Jul 04 '19 at 14:08