Method reference in Java 8
<pre><code>public class Car {

    private int maxSpeed;

    public Car(int maxSpeed) {
        this.maxSpeed = maxSpeed;
    }

    public int getMaxSpeed() {
        return maxSpeed;
    }
}
</code></pre>

<p>We can sort a list of cars by,</p>

<pre><code>    Car carX = new Car(155);
    Car carY = new Car(140);

    List&lt;Car&gt; cars = new ArrayList&lt;&gt;();
    cars.add(carX);
    cars.add(carY);

    cars.sort(Comparator.comparing(Car::getMaxSpeed));
</code></pre>

<p>If we see the signature of the method <code>Comparator.comparing</code>, the input parameter type is <code>Function&lt;? super T, ? extends U&gt;</code> </p>

<p>In the above example, how is <code>Car::getMaxSpeed</code> being cast to <code>Function&lt;? super T, ? extends U&gt;</code> while the following does not compile?</p>

<blockquote>
<pre><code>  Function&lt;Void, Integer&gt; function = Car::getMaxSpeed;
</code></pre>
</blockquote>