The knowledge point in this chapter--Java interface

Source: Internet
Author: User

Because the National Day holiday, last week to learn the knowledge I just know unfamiliar, so summed up the knowledge points in this chapter.
Java interface

Interface (English: Interface), which is an abstract type in the Java programming language, is a collection of abstract methods, and interfaces are usually declared with Interface. A class inherits the abstract method of an interface by inheriting the interface.

Interfaces are not classes, and the way they are written is similar to classes, but they are different concepts. Class describes the properties and methods of an object. The interface contains the methods to be implemented by the class.

Unless the class that implements the interface is an abstract class, the class defines all the methods in the interface.

The interface cannot be instantiated, but it can be implemented. A class that implements an interface must implement all the methods described in the interface, otherwise it must be declared as an abstract class. In addition, in Java, the interface type can be used to declare a variable, they can be a null pointer, or be bound to an object implemented in this interface.
Interfaces are similar to classes:

An interface can have multiple methods.
The interface file is saved in a file that ends in. java, and the filename uses the interface name.
The bytecode file for the interface is saved in a file that ends in a. class.
The corresponding bytecode file for the interface must be in the directory structure that matches the package name.

The difference between an interface and a class:

An interface cannot be used to instantiate an object.
Interface has no constructor method.
All methods in an interface must be abstract methods.
An interface cannot contain member variables, except static and final variables.
Interfaces are not inherited by classes, but are implemented by classes.
The interface supports multiple inheritance.

Interface Features

Each method in the interface is implicitly abstract, and the methods in the interface are implicitly specified as public abstract (only public abstract, and other modifiers will be error-marked).
The interface can contain variables, but the variables in the interface are implicitly specified as public static final variables (and can only be public, with the private modifier reporting compilation errors).
The method in the interface is not implemented in the interface, only the class implementing the interface can implement the method in the interface.

The difference between an abstract class and an interface

1. Methods in an abstract class can have a method body, which is the specific function that can implement the method, but the method in the interface is not.
2. member variables in an abstract class can be of various types, whereas member variables in an interface are only public static final types.
3. Interfaces cannot contain static code blocks and static methods (methods that are decorated with static), whereas abstract classes can have static code blocks and static methods.
4. A class can inherit only one abstract class, while a class may implement multiple interfaces.
You must study well after school.

The knowledge point in this chapter--Java 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.