Interfaces in Java)

Source: Internet
Author: User

1. What is a Java interface )?

Java interfaces are a collection of methods and features, and there is no way to implement them. The methods defined in the Java interface are implemented in different places and can have completely different behaviors (functions ).

Note: (1) Java interfaces cannot be instantiated.

(2) The members declared in the Java interface are automatically set to public, so private members cannot be declared in the Java interface.

(3) The method body implementation cannot appear in the Java interface.

(4) implementing a Java interface must implement all the methods defined in it.

2. Why is Java interface required? What are the benefits of using interfaces?


Interfaces are used to reduce the coupling between codes.

Java is a one-way inheritance language. An interface is used to implement a function similar to multi-inheritance. It can be said that it is another form of Multi-inheritance.

In general, it is to improve the reusability, scalability and maintainability of the program.

Used when common method features exist. Implementation classes have different implementation methods.

2. When to use interfaces and inheritance? When can I abstract a Java interface?

Both of them can bring the benefits of polymorphism. Generally, when the two classes conform to the is-a (Child class is the parent class) relationship, and the Child class can reuse part of the parent class code, the inheritance relationship is used. In other cases, the Java interface is preferred.

One case is the call between the layer and the layer. The other is used for unstable parts.

3. What is interface-oriented programming? What is the interface-oriented programming principle?

During system development, the main architecture uses interfaces to form the skeleton of the system. In this way, you can replace the system implementation by replacing the class that implements the interface.

During running, the Java Virtual Machine calls different methods based on the actual object type.

You can replace the system implementation by replacing the class that implements the interface.

4. Interface-Oriented Programming implementation steps.

(1) Abstract The Java interface. Abstract The common method features of multiple classes into a Java interface and define a common method.

(2) Implement Java interfaces. Each class has different implementations to implement the methods defined in the Java interface.

(3) Use the Java interface. Let interfaces form the skeleton of the system. Defines the method and uses interfaces as parameters. Different objects are instantiated during the call.

5. What is a constant?


Constants are not changed when the program is running. Or a constant is an identifier whose value remains unchanged during running.


Constant identifiers can only be referenced in a program and cannot be assigned a value again.

6. Why do constants need to be used.

To prevent variables from being arbitrarily modified during running.

7. Definition rules of constants in Java.

(1) Add final keywords to the variable declaration to represent constants, and add static keywords to represent class variables.

Public static final int male = "male ";

(2) constants are generally capitalized.

8. Declare constants in the Java interface.


Because the variables declared in the Java interface are automatically added with the public static final modifier during compilation, they are automatically declared as public constants. Therefore, the Java interface is the best place to declare constants.

1. What is a Java interface )?

Java interfaces are a collection of methods and features, and there is no way to implement them. The methods defined in the Java interface are implemented in different places and can have completely different behaviors (functions ).

Note: (1) Java interfaces cannot be instantiated.

(2) The members declared in the Java interface are automatically set to public, so private members cannot be declared in the Java interface.

(3) The method body implementation cannot appear in the Java interface.

(4) implementing a Java interface must implement all the methods defined in it.

2. Why is Java interface required? What are the benefits of using interfaces?


Interfaces are used to reduce the coupling between codes.

Java is a one-way inheritance language. An interface is used to implement a function similar to multi-inheritance. It can be said that it is another form of Multi-inheritance.

In general, it is to improve the reusability, scalability and maintainability of the program.

Used when common method features exist. Implementation classes have different implementation methods.

2. When to use interfaces and inheritance? When can I abstract a Java interface?

Both of them can bring the benefits of polymorphism. Generally, when the two classes conform to the is-a (Child class is the parent class) relationship, and the Child class can reuse part of the parent class code, the inheritance relationship is used. In other cases, the Java interface is preferred.

One case is the call between the layer and the layer. The other is used for unstable parts.

3. What is interface-oriented programming? What is the interface-oriented programming principle?

During system development, the main architecture uses interfaces to form the skeleton of the system. In this way, you can replace the system implementation by replacing the class that implements the interface.

During running, the Java Virtual Machine calls different methods based on the actual object type.

You can replace the system implementation by replacing the class that implements the interface.

4. Interface-Oriented Programming implementation steps.

(1) Abstract The Java interface. Abstract The common method features of multiple classes into a Java interface and define a common method.

(2) Implement Java interfaces. Each class has different implementations to implement the methods defined in the Java interface.

(3) Use the Java interface. Let interfaces form the skeleton of the system. Defines the method and uses interfaces as parameters. Different objects are instantiated during the call.

5. What is a constant?


Constants are not changed when the program is running. Or a constant is an identifier whose value remains unchanged during running.


Constant identifiers can only be referenced in a program and cannot be assigned a value again.

6. Why do constants need to be used.

To prevent variables from being arbitrarily modified during running.

7. Definition rules of constants in Java.

(1) Add final keywords to the variable declaration to represent constants, and add static keywords to represent class variables.

Public static final int male = "male ";

(2) constants are generally capitalized.

8. Declare constants in the Java interface.


Because the variables declared in the Java interface are automatically added with the public static final modifier during compilation, they are automatically declared as public constants. Therefore, the Java interface is the best place to declare constants.

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.