Identity type mode

Source: Internet
Author: User

Defines an interface that does not contain any methods, and uses it only to represent an abstract type. All classes that implement the interface imply that this type belongs.

For example, define a food interface, which does not contain any methods:

 Public Interface food{}// the classes that implement the interface are food types

Fish:

 Public class Implements Food{...}

How to Eat:

 Public void eat (food food) {...}

Eating:

New Fish (); // fish implements the food interface and identifies its type eat (fish); // Legal New Book (); // Book does not implement food interface eat (book); // Compile Error

The so-called identity type pattern is a semantic constraint on the food parameters passed to the Eat () method with the help of the Java compiler. The food interface is known as the identity type interface, which has no methods and represents only an abstract type. In the JDK, there are two typical representations of type interfaces.

    • Java.io.Serializable interface: An instance of a class that implements the interface can be serialized
    • Java.io.Remote interface: An instance of a class that implements the interface can be used as a remote object

Identity type mode

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.