Overview of interfaces in the Java language

Source: Internet
Author: User

Interface

The member modifiers in the interface are fixed

1, Global constant: public staticfinal

2, abstract method: Public abstract

The members in the interface are public permissions.

A class is an inheritance relationship between a class and a class, and an implementation relationship between classes and interfaces.

Interfaces cannot be instantiated

The subclass can only be instantiated if it has a subclass that implements the interface and overrides all the abstract methods in the interface. Otherwise, this subclass is an abstract class.

When you invoke the constant of an interface, the interface name, the subclass name of the interface, and the object of the subclass can be called.

Multiple inheritance is not directly supported in Java because of the uncertainty of the call.

So Java improved the multi-inheritance mechanism and became a multi-implementation in Java.

The following is an example of an interface implementation problem.

Interface aa{public         void Show (); Interface bb{public         int Show (); Class IMP implements aa,bb{public       Void Show () {}public int show () {}//There will be two semantics}

Error: IMP is not abstract and does not overwrite the abstract method in BB show ()

Show () in IMP could not implement show () in BB; Attempting to use incompatible return type

A class can also implement multiple interfaces while inheriting another class. The presence of interfaces avoids the limitations of single inheritance.

Features of the interface

Interface is the rule of external exposure

Interface is a function extension of a program

The presence of interfaces reduces coupling

Interfaces can be used to implement multiple

A class is an implementation relationship between interfaces, and a class can inherit a class and implement multiple interfaces at the same time.

An interface can have an inheritance relationship with an interface, and it can be a multiple-inheritance relationship.

Similarities and differences between interfaces and abstract classes

The same point: it's always drawn upward.

Different points:

1, abstract classes need to be inherited, and can only be inherited by single

Interfaces need to be implemented and can be implemented more

2. Abstract methods and non-abstract methods can be defined in an abstract class, and after the subclass inherits, the non-abstract method can be used directly

Only abstract methods can be defined in an interface and must be implemented by subclasses.

3, abstract class integration is an is a relationship, in the definition of the basic common content of the system

The implementation of the interface is like a relationship, in defining the additional functionality of the system.

Overview of interfaces in the Java language

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.