Internal interface in "simple Java" Java

Source: Internet
Author: User

What is an internal interface

An internal interface, also known as a nested interface, defines another interface within one interface. For example, the entry interface is defined in the map interface with the following code:

 Public Interface Map {    interface  entry{        int  getKey ();    }     void clear ();}
Why use an internal interface

Here are some strong reasons:

A logical grouping of interfaces that are used in the same place;

The embodiment of the encapsulation thought;

Nested interfaces can enhance the readability and maintainability of code;

An example of using an internal interface in the Java Standard library is java.util.Map and Java.util.Map.Entry. Here Java.util.Map is also used as a namespace. Entry is not part of the global scope, which means there is many other entities that is Entries and is not necessary Map ' s Entries. This indicates, Entry represents entries related to the MAP.

How the Inner class works

To figure out how the internal interface works, we can compare it to the inner class. An inner class can be thought of as a general method within the definition of an external class. Because a method can be declared as static and non-static, similar inner classes can also be declared as static and non-static. A static class is similar to a static method, and it can only access static member properties of an external class. A non-static method can access all member properties of an external class.

Because an interface is not instantiated, the internal interface is only meaningful if it is static. Therefore, by default, the internal interface is static, and you cannot manually add the static keyword.

Internal interface Example

Map.java

 Public Interface Map {    interface  entry{        int  getKey ();    }     void clear ();}

Mapimpl.java

 Public class Implements Map {    classimplements  map.entry{        publicint   GetKey () {            return 0;        }    }    @Override    publicvoid  Clear () {        //Clear     }}

Link: http://www.programcreek.com/2013/08/inner-interface-in-java/

Internal interface in "simple Java" Java

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.