Interface knowledge Rollup in Java _java

Source: Internet
Author: User

Why should I use an interface?

If there is a demand: to achieve the security door function. The door has the function of "open" and "off", the lock has the function of "lock" and "unlock".

Analysis: First security door is a gate, door has the function of opening and closing, there is a lock, lock unlock and locked, in accordance with object-oriented programming ideas, we will be the door and lock as a class and separate existence, but, can not let the security door inherited from the gate at the same time inherit self-locking, anti-theft door is not a lock, does not conform to the inheritance is A, which supports single inheritance in Java. So how do we solve this problem, then we need to use the interface.

Two. What is an interface

Interfaces in software are a specification and standard that they can constrain the behavior of a class, is a collection of method features, but there is no method of implementation, the interface can actually be seen as a special abstract class, but with the abstract class is completely different from the design concept is different, abstract class is conducive to code reuse, Interfaces facilitate the extension and maintenance of code.

Three. The difference between abstract classes and interfaces:

01 Abstract classes can provide the implementation details of the member method, while the public abstract method can only exist in the interface;

02. The member variables in the abstract class can be of various types, while the member variables in the interface can only be public static final types;

03. The interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;

04. A class can inherit only one abstract class, while a class may implement multiple interfaces.

Four. How to define an interface

First, let's look at the syntax:

[Modifier] Interface interface Name extends parent interface 1, parent interface 2, ...

{

Constant definition

Method definition

}

Implement interface syntax in a class:

Class name parent class name implements Interface 1, interface 2, ...

{

Class Members

}

Five. Define interface attention points

01. The naming rules for interfaces are the same as classes, and if the modifier is public, the interface is visible throughout the project, and if the modifier is omitted, the interface is only visible in the current package.

02. You can define constants in an interface, you cannot define variables, and if you define attributes in an interface, you can see by decompile that he will automatically use the public static final modifier, the properties in the interface are global static constants, and the constants in the interface must specify the initial values when defining.

03. All methods in the interface are abstract methods, and the methods in the interface are automatically decorated with public abstract, that is, only the global abstract method in the interface.

04. The interface cannot be instantiated and cannot be constructed in the interface.

05 interfaces can be implemented through extends, an interface can inherit multiple interfaces, but interfaces cannot inherit classes.

    06. The implementation class of an interface must implement all the methods of the interface, otherwise it must be defined as an abstract class.

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.