felipeek
felipeek

Reputation: 1213

Why InputStream methods are not abstract?

The java.io documentation says InputStream methods like close(), mark(), available() and others are not abstract. However, according to Java documentation, they don't do anything in the default implementation. My question is: Is there a reason for it? I mean, if this methods do nothing by default, obviously I'll have to rewrite them if I decide to create an InputStream subclass. So, in my vision, making these methods abstract would be more correct, since if we don't do that I can easily forget to rewrite one of these methods and it will not work correctly.

Upvotes: 0

Views: 216

Answers (2)

SparkOn
SparkOn

Reputation: 8956

Marking a method as abstract in a class ensures that a subclass must provide implementation of the abstract method otherwise the subclass shall be made an abstract class. So there is not such necessary for the sub-class of Inputstream class to provide the implementation of that methods.

Upvotes: 0

The purpose of not making them abstract is precisely so you don't have to put in empty method bodies if you're not going to support optional operations. "Doing nothing" is different from "having a default behavior", which in the case of InputStream varies from actually nothing (mark is a no-op, since marks aren't always supported) to throwing an exception to indicate inability to perform the requested operation (reset). Any stream that doesn't provide those facilities will use this same behavior, and any stream that does support them will have to override with the specific implementation anyway.

Upvotes: 4

Related Questions