Why FilterInputStream is not Abstract Class

637 views Asked by At

we cannot instantiate java.io.FilterInputStream as it has no public constructor, we even don't need to without extending it.

why didn't they make that an abstract class?

3

There are 3 answers

5
Ian Roberts On BEST ANSWER

Technically protected in Java includes both subclasses of the class in question, and other classes (not necessarily subclasses) that are in the same package. So it would be possible for another class in java.io to directly instantiate a FilterInputStream. The only reason I can think for why you'd want to do that is that FilterInputStream.read(byte[] b) delegates to this.read(b, 0, b.length) rather than delegate.read(b) so it's a way to prevent the delegate's read(byte[]) method from ever getting called. But that's all speculation on my part.

Other than that specific case, as you note it is effectively abstract, but it doesn't have any abstract methods so the compiler would not require that the class be declared abstract.

0
user207421 On

we cannot instantiate java.io.FilterInputStream as it has no public constructor

We cannot. Classes in the same package can.

0
Pushpendra On

FilterInputStream does not have a public constructor (it is protected - You can see the javadoc), so you can't instantiate it directly. As i have studies that DataInputStream is derived from FilterInputStream (and DataInputStream does have a public constructor), so it can be instantiated.