I'm trying to understand abstract classes in Java so I wrote this code snippet in IntelliJ:
AutoCloseable ac = new BufferedInputStream(new InputStream() {
@Override
public int read() throws IOException {
return 0;
}
});
The @Override
and read()
stub was created automatically by IntelliJ.
Since InputStream
is an abstract class, why can I instantiate it with the new
keyword?
And another thing. When I delete the method stub like this:
AutoCloseable ac = new BufferedInputStream(new InputStream());
The IDE says that InputStream
is abstract and therefore can't be instantiated (as expected).
So, why is the former valid and the latter not?
And where does this read()
method come from?