C # What is the difference between interfaces and Java?

Source: Internet
Author: User
ArticleDirectory
    • Implement multiple interfaces

An interface is a "main class" that contains method signatures but does not implement methods. In this respect, interfaces and abstract classes only contain abstract methods. C # interfaces are very similar to Java interfaces and basically work in the same way.

All members of an interface are defined as public members, and the interface cannot contain constants, fields (private data members), constructors, destructor, or static members of any type. If any modifier is specified for the interface member, the compiler will generate an error.

To implement the interface, we can derived from the interface. Such a derived class must be implemented for all interface methods unless declared as abstract.

The interface declaration is exactly the same as that of Java. In the interface definition, the get and set keywords are used separately. The attribute only indicates its type, and whether it is read-only, write-only, or read-write. The following interface declares a read-only attribute:

1 Public   Interface Imethodinterface
2 {
3 // Method signatures
4 Void Methoda ();
5 Int Methodb ( Float Parameter1, Bool Parameter2 );
6
7 // Properties
8 Int Readonlyproperty
9 {
10Get;
11}
12 }
13
14

A colon is used to replace the JAVA Implementation keyword, and the class can inherit this interface. The implementation class must provide the definition of all methods and any required property accessors:

1 Public   Class Interfaceimplementation: imethodinterface
2 {
3 // Fields
4 Private   Int Count =   0 ;
5 Private   Int ID;
6
7 // Implement methods defined in Interface
8 Public   Void Methoda ()
9 {
10
11}
12
13 Public   Int Methodb ( Float Parameter1, Bool Parameter2)
14 {
15
16ReturnIntegervariable;
17}
18
19 Public   Int Readonlyproperty
20 {
21 Get
22 {
23ReturnCount;
24}
25 }
26
27 // Add extra methods if required
28
29 }
30
31

Implement multiple interfaces

By using the following syntax, a class can implement multiple interfaces:

Public   Class Myclass: interfacename1, interfacename2, interfacename3

If a class implements multiple interfaces, the member name will have ambiguity. This problem can be solved by using the full qualifier of the attribute or method name. In other words, the derived class can resolve this conflict by using a fully qualified name of the method to indicate which interface it belongs to (for example, imethodinterface. methoda.

Article from: http://www.microsoft.com/china/msdn/library/langtool/vcsharp/Usgettingstartedcsharpforjava.mspx? MFR = true

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.