java-Base-Interface

Source: Internet
Author: User

Interface

An interface is a collection of functions, which can also be seen as a data type, which is a more abstract class than an abstract class.

The interface only describes the methods that should be available, and there is no specific implementation, the implementation of the interface is implemented by the class (equivalent to the subclass of the interface) to complete. This separates the definition and implementation of the function and optimizes the program design.

All things have function, that is, everything has an interface.

definition of the interface:

Unlike the class that defines classes, the interface keyword is required for the interface definition.

The definition of the interface is still a. java file, although a. class file is still generated after the compilation of the interface keyword that is used when declaring. This allows us to consider an interface as a special class that contains only functional declarations.

Define the format:

Public interface Interface Name {

Abstract Method 1;

Abstract Method 2;

Abstract Method 3;

}

Using interface instead of the original class, the other steps are the same as defining the class:

The methods in the L interface are abstract methods of public access

Normal member variables cannot be defined in the L interface

Class implementation Interface

The relationship of a class to an interface is to implement the relationship, that is, the class implements the interface. The implemented action is similar to inheritance, except that the keywords are different and implemented using implements.

When the other class (Implementation Class) implements the interface, it is equivalent to declaring: "I should have the functionality in this interface." The implementation class still needs to override the method to implement the specific functionality.

Format:

Class implements interface {

Overriding methods in an interface

}

After the class implements the interface, the class inherits the abstract method from the interface, and the class needs to override the abstract method to complete the specific logic.

The L interface defines a feature that, when needed, allows the class to implement the interface, declaring only that the method should be available, which is a function declaration.

It is the implementation of the method to override the method in the concrete implementation class and implement the function.

Therefore, through the above two actions will be the function of the Declaration and implementation will be separated. (Think again: The class is a description of the real thing, and the interface is a collection of functions.) )

Features of the members in the interface:

1, the interface can be defined variables, but the variable must have a fixed modifier decoration, public static final so the variable in the interface is also called a constant, its value can not be changed. We'll explain the static and final keywords later

2, the interface can be defined methods, methods also have fixed modifiers, public abstract

3. Interfaces are not available to create objects.

4, subclasses must overwrite all the abstract methods in the interface, subclasses can be instantiated. Otherwise, the subclass is an abstract class.

InterfaceDemo {///define a name called Demo interface.      Public Static Final intNUM = 3;//the value of num cannot be changed     Public Abstract voidShow1 ();  Public Abstract voidshow2 ();}//define subclasses to override methods in the interface. The relationship between a class and an interface is an implementation. by keyword implementsclassDemoimplImplementsDemo {//sub-class implementation of the demo interface. //overrides the method in the interface.      Public voidShow1 () {} Public voidShow2 () {}}
Multiple implementations of interfaces

Interface The most important embodiment: to solve the disadvantages of multiple inheritance. This mechanism of multi-inheritance is done in Java through multiple implementations.

Interface fu1{    void  show1 ();} Interface fu2{    void  show2 ();} class Implements FU1,FU2//  multiple implementations. Implement multiple interfaces at the same time.  {    publicvoid  Show1 () {}    publicvoid  Show2 () {}}

How to solve the disadvantages of multiple inheritance?

Cons: When multiple inheritance occurs, when multiple parents have the same functionality, the subclass invocation creates uncertainty.

In fact, the core reason is that multiple inheritance of the function in the parent class has a principal, which causes the call runtime to not determine which principal content to run.

Why do more implementations work?

Because the functionality in the interface has no method body, it is explicit by subclasses.

Class-Inheriting classes implement interfaces at the same time

When a class has inherited a parent class and it needs to extend additional functionality, the interface comes in handy.

Subclasses extend functionality by inheriting the parent class, and the functionality that is extended through inheritance is the underlying functionality that subclasses should have. What if subclasses want to continue extending functionality in other classes? This is done by implementing an interface.

class Fu {    publicvoid  Show () {}}interface  Inter {      Abstractvoid  show1 ();} class extends Implements Inter {    publicvoid  Show1 () {    }}

The presence of interfaces avoids the limitations of single inheritance. The basic functionality of the things defined in the parent class. The extended functionality of the things defined in the interface.

Multiple inheritance of interfaces

You can use extends to inherit between multiple interfaces.

Interface fu1{    void  Show (); Interface fu2{    void  show1 ();} Interface fu3{    void  show2 ();} Interface extends fu1,fu2,fu3{    void  show3 ();}

In the development if there are multiple interfaces in the same way, then if there is a class implementation of these interfaces, then the implementation of the interface method, because the interface method is an abstract method, the sub-class implementation will not occur after the call of uncertainty.

Benefits of the interface:

The interface has an extension function

The appearance of the interface reduces the coupling, that is, the equipment and equipment between the understanding of the decoupling.

The appearance of the interface is convenient for later use and maintenance, the interface is actually an exposed rule.

Features of interfaces and abstractions:

are located at the top of the inheritance and are used by other classes to implement or inherit

Cannot instantiate an object directly

All contain abstract methods, and their subclasses must override these abstract methods.

Difference:

Abstract classes provide implementations for partial methods, which prevent subclasses from repeating these methods, improving code reuse, and interfaces containing only abstract methods.

A class can inherit only one direct parent class (perhaps an abstract class) but may implement multiple interfaces, avoiding the single inheritance of Java

Abstract class is the content that should have in this thing, the inheritance system is the relation of IS...A

Interface is the extra content of this thing, the inheritance system is the like...a relationship

The choice of the two:

Preferred interface, less abstract class

Abstract classes are used when you need to define the behavior of subclasses and also provide common functionality for subclasses.

java-Base-Interface

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.