Grzegorz Wilanowski wrote:. . . there is an abstract method:
public abstract int read() throws IOException
What is the functional impact "throws IOException" to this method?
It means that method and its overriding methods might throw an
IOException, and any code calling that method must be prepared to handle an
IOException. Remember it is incorrect programming to cause an overriding method to throw an exception that is not thrown by its overridden (superclass) version.
Overriding this method in child class:
I know lots of people say, “child class,” but that term isn't accurate. Inheritance in programming terms is different from inheritance in biological terms or inheritance in money terms. Say, “subclass,” and say, “superclass” instead of “parent class”.
1) if I omit "public", I will get error . . .
An overriding method must be accessible to all code that can access the overridden method. You cannot make the method “disappear” from visibility by omitting its access modifier or making its access more restrictive.
2) If I change int to long, I will get error . . .
An overriding method must return a value that its calling code can handle. The calling code can handle an
int, but it cannot handle a
long. So you need the same return type (I think) as the overridden method, except that for reference types the return type may be covariant. The return type may be a subtype of the overridden method's return type. There is a concept of methods being return type substitutable, which should appear in the JLS (=Java® Language Specification)
here.
3) But I can omit "throws IOException" with no error.
Yes, the overriding method can be an improvement and not throw any exceptions. It is incorrect to throw a new kind of exception. The calling method “expects” the method to run to completion by returning a value or throwing a particular exception, and can handle that. It is incorrect practice to throw another kind of exception because the calling code cannot handle that. Not even an
unchecked exception like
ArithmeticException. Remember that the compiler goes through the different kinds of exception and the clause
throws SomeUncheckedExceptionType has no effect. The fact that the compiler ignores a particular exception declaration doesn't make it good programming.
* * * * * * * * * * * * * * * * * * * * * * * * *
I suggest you try a few programs using
InputStream#read(), until you develop antibodies to it and never want to see it again See what happens if you inflict letters like Ł on
read(). You will have to work out your own encoding It is a dreadful method to use and you should leave it to things like
BufferedReader#readLine() to use
read(). That sort of method doubtless uses
read(), so you don't have to. But
readLine() doesn't handle any IOExceptions, so you will still have to handle that exception.
* * * * * * * * * * * * * * * * * * * * * * * * *
Remember that the overriding method should do what the overridden method does, and mustn't do it worse, but may do it better. That also means the overriding method should comply with all of the specification (=documentation) for the overridden method.