Tuesday, November 30, 2021

Closeable Interface in Java

 A Closeable Interface is a source or destination of the data that needs to be closed. The close method is invoked to release resources that the object is holding (such as open files).

public interface Closeable
extends AutoCloseable

This Interface was introduced in JDK 5 and defined in the java.io. Now From JDK 7+, we can use AutoCloseable interface. The closeable interface is an older interface that was introduced to preserve backward compatibility.

Declaration
public interface Closeable extends AutoCloseable {
    public void close() throws IOException;
}
Example: - Implement the Closeable interface
import java.io.Closeable;
import java.io.IOException;

public class CloseableExampleClass implements Closeable {

    @Override
    public void close() throws IOException {
        // close resource
        System.out.println("CloseableExampleClass Oblect Close.");
    }
}
Q:- What are the superInterface of Closeable? 
Ans: AutoCloseable 

Q:- What are the subInterfaces of Closeable? 
Ans: Below are subInterfaces of Closeable 
  • AsynchronousByteChannel 
  • AsynchronousChannel 
  • ByteChannel Channel 
  • ImageInputStream 
  • ImageOutputStream 
  • MulticastChannel 

Q:- What are the Implementing Classes? 
Ans: Below are Implementing Classes 
  • AbstractSelectableChannel 
  • AbstractSelector 
  • BufferedInputStream 
  • BufferedOutputStream 
  • CheckedInputStream 
  • CheckedOutputStream 
  • BufferedReader 
  • BufferedWriter

No comments:

Post a Comment