Classes and Interfaces (iii) interfaces in Java and nested interfaces

Source: Internet
Author: User
Tags class definition

One, interface 1. Introduction to Interfaces

interface: is an abstract type of Java and a collection of abstract methods. An abstract type in which an interface is more abstract than an abstract class.

Interface Syntax:

[修饰符] [abstractinterface 接口名 [extends 父接口名列表]{[public] [static] [final] 常量;[public] [abstract] 方法;[public] [static] 方法{};//JDK1.8后[public] [static] 类/接口;}

2. Modifiers for interface members

The access rights of all members in the interface are enforced by defaultpublic

    • variables: default coercion ispublic static final
    • method: The default coercion is public abstract the abstract method. But after JDK1.8, the static method is allowed to be defined public static , and the interface exists non-abstract method;
    • internal class/interface: default coercion ispublic static

The interfaces in the following example define the various interface members:

 Public Interfaceinterfacetest {intA =Ten;//global variable default mandatory is public static final          Public Static void Staticmethod() {//static method, JDK1.8 compiled bySystem. out.println(""); }int Abstractmethod();//Abstract method           Public Static Abstract classinnerclass{//Abstract inner class, forced public static by default         //......}enumMyenum{red,blue,grren}//enum class, forcing public static by default          Interfaceinnerinteerface{//nested interface, default force public static         void AA(); }}classMyClassImplementsinterfacetest{//Implement the above interface    @Override     Public int Abstractmethod() {//Implement abstract methods                return 0; }}

Note that implementing an interface does not need to implement an internal abstract class member inside an interface, or a nested interface member;

3. Multiple inheritance of interfaces

?? An interface is one that can inherit multiple interfaces (in a class, which is not allowed to inherit multiple). Because an instance member method of an interface is abstract, multiple inheritance does not affect the interface too much (different parent interfaces can define the same method), but there is a point to note that when multiple inheritance interfaces, the method inherited from the parent interface has the same method name, but does not satisfy the condition of the method overload, it will conflict. If this occurs, the compilation fails and multiple inheritance is not allowed.

interface A{    voidsameMethodA();}interface B{    voidsameMethodA();    voidsameMethodB(int a);//返回类型是void}interface C{    intsameMethodB(int a);//返回类型是 int}interfaceextends//编译通过,即使A,B定义了相同方法    }interfaceextends B,C{//编译失败    }

Word interface D multiple inherited parent interface A, a success, but sub-interface E is more inherited from the parent interface B,c failed, because the two interfaces defined in sameMethodB() addition to the return type, the method name, the parameter list is the same, this does not satisfy the method overload, resulting in a method conflict inheritance.

If you have to inherit more, the solution for Java is to use an internal class or nested interface;

4. The difference between an interface and an abstract class

The difference between an interface and an abstract class:

    • The level of abstraction is different: the interface is more abstract, only the abstract method is allowed before JDK1.8, the static method is allowed after the jdk1,8, the abstraction of the abstract class is basically low, allowing concrete methods to exist, even without abstract methods in the abstract class. But the class with abstract method must be abstract class;
    • An interface has a fixed restriction on the definition of a member, referring to the preceding; the abstract class definition member is the same as the normal class;
    • The initialization block (static or instance) is not allowed in the interface, but the abstract class allows the definition of both initialization blocks;
    • The constructor method cannot be defined in an interface, but an abstract class allows you to define a constructor method.

Add: Abstract methods in abstract classes have access rights that cannot be private .

Two, nested interface

nested interfaces: is the interface defined in a class or interface.

Modifier restrictions for nested interfaces:

    • Whether defined in an interface or a class, the nested interface is forced by default static . This means that a nested interface is a nested interface that has no local.
    • The interface definition in the class, you can use four kinds of access rights, defined in the interface species, then onlypublic

Classes and Interfaces (iii) interfaces in Java and nested interfaces

Related Article

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.