Why are there no abstract methods for these Java interfaces? Talking about Java Tag interface

Source: Internet
Author: User
Tags unsupported

In the JDK source code, there are some interfaces, they do not contain any (abstract) methods, but there are widespread.

This interface we call Mark Interface, which is the tag interface.

These interfaces, we do not have to implement any method, their role is when a class implementation of this interface, we think this class has a certain function of this interface tag.

Here are three examples of three tag interfaces commonly used in Java:

Randomaccess, Cloneable, java.io.Serializable

(1) randomaccess

In C #, there are often many people arguing about whether to use for or foreach when traversing a collection.

In Java, there is no need to dwell on this problem at all:

There is such an interface in Java

1  Public Interface randomaccess {2 }

The purpose of this interface is to determine whether a set can be accessed quickly. That is, when an index is passed in, the pointer can move quickly to the corresponding element, or it needs to be moved to the specified element in the same order as the access queue.

If we implement a container class, the container (anti-theft connection: This article starting from http://www.cnblogs.com/jilodream/) elements can be quickly accessed through the index (the general core of the storage interface is the use of arrays), then you at the definition of the class, You can hit the implementation label of the Randomaccess interface like ArrayList:

1  Public classArraylist<e>extendsAbstractlist<e>2         ImplementsList<e>, Randomaccess, cloneable, java.io.Serializable3 {4 //...5 }6 and LinkedList does not implement the tag interface7  Public classLinkedlist<e>8     extendsAbstractsequentiallist<e>9     ImplementsList<e>, deque<e>, cloneable, java.io.SerializableTen { One //... A}

In the process of use, by judging whether the implementation of the Randomaccess interface, you can decide what kind of traversal to take.

As follows:

1 Importjava.util.List;2 Importjava.util.RandomAccess;3 4  Public classsourcelearning5 {6      Public voidIteratorelements (list<string>list)7     {8         if(Listinstanceofrandomaccess)9         {Ten              for(inti = 0, size = list.size (); i < size; i++) One             { AString element =List.get (i); -             } -         } the         Else -         { -              for(String element:list) -             { +             } -         } +     }     A}

This allows the traversal to be made faster by taking different traversal forms for different lists.

(2) cloneable

This interface is very familiar to everyone, in the deep copy, often used to this interface. This interface is also a markup interface:

1  Public Interface cloneable {2 }

His role is to mark whether the object has the ability to clone. Many may feel puzzled, the object class itself has been implemented protected native Object clone () throws Clonenotsupportedexception;

Method

As a matter of principle, each class should be able to run the Clone method, why do you need to hit such an interface. The advantage of this is that the object is labeled as an interface that has some kind of capability. Think about how we do it in our usual work, if we don't do it in the form of a labeled interface. In general, it is controlled by setting enumerations or by adding variables. This may solve the problem, but it is often not possible to solve the problem gracefully from an object-oriented perspective.

Think about what the interface is for, right? An interface is a way to mark what features and features a class has. The markup interface is an abstraction at a higher level in an object-oriented perspective: Even if you have this method, you do not have the markup interface for this function.

So in the process of invoking clone, if the object does not implement the Cloneable interface, then the dummy throws a clonenotsupportedexception, an unsupported clone exception.

(3) Java.io.Serializable

This interface is used to mark whether a class supports serialization. The so-called serialization is to transform the object's various information into a form that can be stored or transmitted. I remember when I first came to work, the serialization is very difficult to understand, I think the server returned an object, the client can receive, why always (anti-theft connection: This article starting from http://www.cnblogs.com/jilodream/) serialization, Deserialization of the toss. Later leader told me this is because a lot of times, because of the communication protocol, in the process of transmission, complex class object is not supported by the transmission, so generally to be converted into the form of a stream, put in the package to pass. At the same time, java.io.Serializable and cloneable interface, if a class does not implement the interface, and is taken to serialize, the virtual machine will throw an unsupported exception, although from the code call, there is no problem.

See the second thrown exception in the Java source Comment: An object that will be serialized but not implemented with the serialized interface:

1      /**2 * Write The specified object to the ObjectOutputStream. The class of the3 * object, the signature of the class, and the values of the Non-transient4 * and non-static fields of the class and all of their supertypes are5 * written. Default serialization for a class can overridden using the6 * WriteObject and the readobject methods. Objects referenced by this7 * object is written transitively so, a complete equivalent graph of8 * objects can is reconstructed by an objectinputstream.9      *Ten * <p>exceptions is thrown for problems with the outputstream and for One * Classes that should not being serialized. All exceptions is fatal to the A * OutputStream, which is a indeterminate state, and it 's - * The caller to ignore or recover the stream state. -      * the      * @throwsinvalidclassexception Something is wrong with a class used by - * Serialization. -      * @throwsnotserializableexception Some object to being serialized does not - * Implement the Java.io.Serializable interface. +      * @throwsIOException Any exception thrown by the underlying - * OutputStream. +      */ A      Public Final voidWriteObject (Object obj)throwsIOException { at         if(enableoverride) { - writeobjectoverride (obj); -             return; -         } -         Try { -WriteObject0 (obj,false); in}Catch(IOException ex) { -             if(Depth = = 0) { to Writefatalexception (ex); +             } -             Throwex; the         } *}

At this point, through the three commonly used marking interfaces, the use of the clear Mark interface should already be elaborated, the individual thinks this is a very abstract object-oriented way. It is much more elegant to tag an object only by adding a label to it, or not to implement a function, rather than directly defining a variable or enumeration.

Why are there no abstract methods for these Java interfaces? Talking about Java Tag interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.