Home > Java > JavaBase > body text

The difference between c# interface and java interface

王林
Release: 2019-11-19 14:45:15
Original
4117 people have browsed it

The difference between c# interface and java interface

1. Define members in the interface

C#. As shown in the figure, I added a field n to the interface ITest, then vs directly displays a red bottom line, and the error is The interface cannot contain the field

The difference between c# interface and java interface

java, as shown below. The compilation also reports an error, but it is not that the interface cannot be included but that the assignment is missing, so let's assign the value and see again. You can see that there is no problem, that is, the member variables are public static final by default. We know that final must be assigned a value, so if it is not assigned, the place will be compiled.

The difference between c# interface and java interface

After the assignment, it was found that there was no problem, as shown below:

The difference between c# interface and java interface

2. Define some methods in the interface

C#, we all know that the methods in an interface need to be implemented by subclasses, so our methods in the interface cannot be implemented. If an implementation is added, there will be an error. The error is that the interface cannot be defined.

The difference between c# interface and java interface

#Java, that is, jdk1.8, has added some methods that can be implemented. It can be implemented in default and static. As shown below, there is no problem compiling. So does c# have default and static methods? In the interface, sorry there are none.

The difference between c# interface and java interface

# It is not available in c#. For static, default interface methods cannot be defined. Because it is invalid.

The difference between c# interface and java interface

Summary:

1. In java, interfaces can contain fields, but these fields are implicitly static and final, which is not allowed in C# If there are fields in the interface, the compiler will prompt an error when compiling;

2. In Java, method declarations can have public modifiers (even if this is not necessary), but in C#, explicitly It is illegal to specify the public modifier for a method in an interface.

Recommended tutorial: Getting started with java development

The above is the detailed content of The difference between c# interface and java interface. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template