Commonly used Java -able Interfaces

Commonly used Java -able Interfaces

For a Java Developer, it is really important to know the huge array of interfaces and classes which ends with -able in its names. So, why do we have interfaces with such a name? Is it really difficult to answer? I don’t think so. We all know about interfaces like Serializable which assists a class object to be serialized. Apart from this, we still do have several interfaces and classes which have such a naming convention.

Serializable

The class implementing java.io.Serializable interface enables its objects to be serialized. This interface doesn’t have any method. Such an interface is called Marker Interface.

If the classes need to handle the serialization and deserialization in a different way, the class implementing java.io.Serializable must implement special method.

private void writeObject(java.io.ObjectOutputStream out) throws IOException

private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;

private void readObjectNoData() throws ObjectStreamException;

Cloneable

The class that implements java.lang.Cloneable assures that it conforms to the contract of Object.clone() method to copy the instances of the class.

Readable

If a class implements java.lang.Readable assures that it provides source of characters to read from. The user can read the source of characters using its read() method.

Appendable

This java.lang.Appendable interface specifies the contract to append character values to the implementing class object. As an example, the widely used java.lang.StringBuffer and java.lang.StringBuilder implement this interface to mutate the sequence of characters.

Closeable

java.io.Closeable interface provides the contract for the user to close a source or a destination of data. It declares its contract through close() method which is invoked to release the currently acquired resources.

AutoCloseable

java.io.AutoCloseable has been introduces lately in Java 1.7 which ensures the source or destination of data implementing java.io.AutoCloseable interface can be used in accordance with try with resources block. The benefit of using try with resources block is that it automatically closes the data source and it releases the current resources.

Observable

This class ensures an object to be monitored in the Model-View Controller programming paradigm.

Iterable

Implementing this interface allows an object to be the target of the foreach statement.

Comparable

By implementing this interface, the class has the opportunity to compare the object with another specified object conforming to the order.

Runnable

This interface is mainly used to facilitate users to provide Thread Task definition. The difficulty of using this SAM interface for Thread Task Definition is that it doesn’t return any value.

Callable

This interface is also used to provide Thread Task definition in a parameterized way which has the capability to return the computed value as well.

Repeatable

This is basically an annotation which has been introduced in Java 8. This annotation ensures to use it multiple times as mentioned below.

public final class Sample {

	@Repeatable(value = TechnicalUniversities.class)
	public @interface Place {
		String value();

	}

	@Retention(RetentionPolicy.RUNTIME)
	public @interface TechnicalUniversities {
		Place[] value() default {};
	}

	@Place("Munich")
	@Place("Berlin")
	@Place("Freiburg")
	@Place("Stuttgart")
	public interface TechnicalUniversity {

	};
}

Prior to Java 8, we could achieve the same by annotating TechnicalUniversity interface with an array of Places which looks a bit cluttered.

要查看或添加评论,请登录

Ahmed El-Sayed的更多文章

  • Open System Architecture

    Open System Architecture

    "Open System Architecture" typically refers to the design and implementation of systems using open standards, open…

  • ChatGPT: Revolutionizing Conversational AI

    ChatGPT: Revolutionizing Conversational AI

    Artificial intelligence (AI) has come a long way in recent years, and one of the most exciting developments in this…

  • Togaf 9.2 Level 1 (Part 1)

    Togaf 9.2 Level 1 (Part 1)

    efore we go in details , we have to know how can we read the open group standards document, you should download the…

    1 条评论
  • Kafka vs RabbitMQ

    Kafka vs RabbitMQ

    What’s the Difference Between a Message Broker and a Publish/Subscribe (Pub/Sub) Messaging System? Message brokers are…

  • What is the strangler pattern and how does it work?

    What is the strangler pattern and how does it work?

    What is the strangler pattern? Picture a motorcycle that works, but could stand to undergo extensive overhauls that…

  • MIGRATING FROM MONOLITH TO MICROSERVICES: STRATEGY & STEP-BY-STEP GUIDE

    MIGRATING FROM MONOLITH TO MICROSERVICES: STRATEGY & STEP-BY-STEP GUIDE

    Migrating from monolith to microservices is less costly and risky than redeveloping an entire system from scratch. But…

    1 条评论
  • Migrate a monolith application to microservices using DDD

    Migrate a monolith application to microservices using DDD

    A monolithic application is typically an application system in which all of the relevant modules are packaged together…

    1 条评论
  • Migrate From Monolithic To Microservices Using DDD Pattern

    Migrate From Monolithic To Microservices Using DDD Pattern

    The general migration approach has three steps: Stop adding functionality to the monolithic application Split the…

  • Migrate From Monolithic To Microservices Using Strangler Pattern

    Migrate From Monolithic To Microservices Using Strangler Pattern

    There are three steps to transition from a monolithic application to microservices by implementing the Strangler…

  • GraalVM

    GraalVM

    GraalVM has caused a revolution in Java development since it launched three years ago. One of the most discussed…

社区洞察

其他会员也浏览了