Methods in interfaces in Java

Source: Internet
Author: User

the topic is as follows: (Multiple choice) Please select the following interface to define the correct method () A:  Public Static void Main (string[] args); B:privatevoid  test (); C:publicvoid  test2 ();D:staticvoid test3 () {};

The correct answer is: C D.

The reasons are:

A does not provide a default implementation;

B chooses to decorate with private (the private modifier is not allowed in the interface to decorate the method ).

In doing this question, oneself basically is in the state of being in the head, because about the interface oneself only know: interface definition method must be abstract method, do not allow the default implementation.

Today I wrote a demo experiment and found that there are several rules for defining methods in interfaces:

1) in an interface where static methods can be defined, static methods must be implemented . And this static method can only be modified with public or not write.

For example: public static void Test () {} or static void Test () {}.//public omitted does not write by default is also decorated with public.

The method name of a static method can be arbitrary, that is, you can define the main method, for example: public static void Main (string[] args) {}.

2) for normal methods, modifiers are: public, abstract, default, and combined public abstract

Finally, a little description of the properties in the interface

First you need to understand what the interface means. An interface is a uniform ' protocol ', and the attributes in the interface are members of the ' protocol '. They are public, static, and final constants. Equivalent to a global constant.

The variables inside the interface are public static final. So you can write this:

public static final int i=10;

Dolly Mix

int i=10; (Can omit part)

Note that the variable is given an initial value at the time of declaration and cannot be the default.

By comparison, you can see that in the interface is a constant, only the final keyword in the class becomes a constant

Based on the various features of the interface, therefore, what you think is going to change is put in your own implementation, not in the interface, interface is just a kind of things of the properties and behavior of a higher level of abstraction. For the modification to close, the extension (different implementation implements) Open, interface is a manifestation of the principle of closure.

52536233

Methods in interfaces in Java

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.