简体   繁体   中英

Avoid implementing new interface method after JDK upgrade

I've just upgraded some projects from Java 6 to Java 8 and one class was implementing Connection interface. Now Connection interface seems to have more methods but I don't want to implement all missing methods, for example: Connection.getSchema() . Connection.getConnectionTimeOut() and so on.

How I should deal with this issue due to the fact I don't want to make my class abstract or I should implement all the missing methods?

If the Connection interface does not implement Defaultmethods for the given new Methodes you will have to implement them. If they are not used in your Applicationcontext you might be able to just implement them empty. While that will be a quick fix for your problem i would not recommend that because maybe late some other class will have to use these Functions.

You don't have any choice but to implement them.

However, if your custom implementation of Connection is not designed to be general purpose, then you could get away with dummy implementations like this:

public String getSchema() {
    throw new UnsupportedOperationException("Connection::getSchema");
}

And if you discover that you do need some of these methods, you can go back and implement them properly.


Making the class abstract won't help. That just puts the problem off to a subclass ... where you will need to implement the methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM