user6302426
user6302426

Reputation:

Is it important to add AutoCloseable in java?

Is it important to implement AutoCloseable in java?

Would it make sense if I create a class which implements AutoCloseable extends a other class which doesn't implement it?

e.g

public class IGetClosed extends ICannotBeClosed implements AutoCloseable {...}

Upvotes: 2

Views: 6935

Answers (4)

LHA
LHA

Reputation: 9655

Is it important to implement autoCloseable in java?

It is hard to tell it is important or not to implement the interface. But it is not required.

Would it make sense if I create a class which implement AutoCloseable extends a other class which doesn't implement it?

It is OK to do that. Nothing wrong.

AutoCloseable was added from Java 7. It is designed to use with new try-with-resources statement (Java 7+)

See two below classes providing the same functionality. One is not using AutoCloseable and the other is using AutoClosable:

// Not use AutoClosable
public class CloseableImpl {
    public void doSomething() throws Exception { // ... }
    public void close() throws Exception { // ...}
    
    public static void main(String[] args) {
        CloseableImpl impl = new CloseableImpl();
        try {
            impl.doSomething();

        } catch (Exception e) {
            // ex from doSomething
        } finally {
            try { //  impl.close() must be called explicitly
                impl.close();
            } catch (Exception e) { }
        }
    }
}

// Use AutoCloseable 
public class AutoCloseableImpl implements AutoCloseable {
    public void doSomething() throws Exception { // ... }
    public void close() throws Exception { // ...}

    public static void main(String[] args) {
        // impl.close() will be called implicitly

        try (AutoCloseableImpl impl = new AutoCloseableImpl()) {
            impl.doSomething();
        } catch (Exception e) {
          // ex from doSomething or close
        }
    }
}

As you see. Using AutoClosble will make the code shorter and cleaner.

Upvotes: 3

Koos Gadellaa
Koos Gadellaa

Reputation: 1254

From the documentation:

void close() throws Exception

Closes this resource, relinquishing any underlying resources. This method is invoked automatically on objects managed by the try-with-resources statement.

While this interface method is declared to throw Exception, implementers are strongly encouraged to declare concrete implementations of the close method to throw more specific exceptions, or to throw no exception at all if the close operation cannot fail.

So if you want the new class to be automagically closed in a try-with-resources statement, you could thus add that functionality to your ICannotBeClosed class.

Upvotes: 1

Mureinik
Mureinik

Reputation: 311393

There's no problem in having a class implement AutoClosable even if it's super class doesn't. Actually, as all the classes in Java extend java.lang.Object either directly or indirectly, all the AutoClosables eventually extend a class that doesn't implement this interface.

If your class has some semantic of closeing it, you should probably have it implement AutoClosable. It costs almost nothing to do so, if at all, and it allows you to use Java 7's neat syntactic sugaring of the try-with-resource syntax.

Upvotes: 2

Zircon
Zircon

Reputation: 4707

AutoCloseable is an interface that essentially allows an object's resources to be closed automatically when using it in a try-with-resources statement. If you don't plan on using your object with try-with-resources, there is no need to implement it at all.

As a rule of inheritance, no, there is nothing inherently wrong with doing what you want to do. If you want to auto-close resources in your child class, go right ahead.

Upvotes: 1

Related Questions