java - do I have to overwrite all versions of overloaded function in inheriting class?

Paflow :

Because I need a cancelable OutputStreamWriter, id did the following

public class CancelableStreamWriter extends OutputStreamWriter {

    public CancelableStreamWriter(OutputStream out, String charsetName) throws UnsupportedEncodingException {
        super(out, charsetName);
        //stuff
    }

    public CancelableStreamWriter(OutputStream out) {
        super(out);
        //stuff
    }

    public CancelableStreamWriter(OutputStream out, Charset cs) {
        super(out, cs);
        //stuff
    }

    public CancelableStreamWriter(OutputStream out, CharsetEncoder enc) {
        super(out, enc);
        //stuff
    }

    public void write(int c) throws IOException {
        if (myCancelCondition) {
            // cancel stuff
        }
        super.write(c);
    }

    public void write(char cbuf[], int off, int len) throws IOException {
        if (myCancelCondition) {
            // cancel stuff
        }
        super.write(cbuf, off, len);
    }

    public void write(String str, int off, int len) throws IOException {
        if (myCancelCondition) {
            // cancel stuff
        }
        super.write(str, off, len);
    }
}

I wonder, if I really have to overwrite all overloaded versions of the write function. Or is there something possible like:

    public void write(WHATEVERARGS) throws IOException {
        if (myCancelCondition) {
            // cancel stuff
        }
        super.write(WHATEVERARGS);
    }

Same with constrcutors.

Maybe this is not the most elegant approach at all, but I can't use a wrapper object for example, since the owning class is typed to OutputStreamWriter and I can't change that.

Impulse The Fox :

Keep your approach.

Unfortunately, it is not possible and there is no shortcut for overriding all methods (or "overriding" all constructors). Java methods and constructors must have fixed parameters. This is because of language design.

Fortunately, most IDEs support generating such code in a few clicks. You just have to select that you want to override all methods and constructors (and then add your custom functionality).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=98693&siteId=1