Identity type mode

Source: Internet
Author: User

Identity type mode

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{}//实现该接口的类都是食物类型
    • 1


public class Fish implements Food{...}
    • 1

How to Eat:

public void eat(Food food){...}
    • 1


new Fish();//Fish实现了Food接口,标识其食物类型eat(fish);//合法Book book = new Book();//Book未实现Food接口eat(book);//编译错误
    • 1
    • 2
    • 3
    • 4

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.

    • interface: An instance of a class that implements the interface can be serialized
    • 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: 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.