C #-interface PK abstract class

Source: Internet
Author: User

C #-interface PK abstract class

One of the characteristics of object-oriented is abstraction. abstract classes and interfaces have to be mentioned. What are the relationships and differences between abstract classes and interfaces? Let's discuss it.

First, let's take a look at Baidu encyclopedia's explanation:

Abstract class:In object-oriented, all objects are depicted through classes, but in turn, not all classes are used to depict objects, if a class does not contain enough information to depict a specific object, such a class is an abstract class.

Interface:A program interface consists of a set of statements, functions, options, other forms of expressing the program structure, and data provided by the program or program language used by the programmer.

Definition method:

Abstract class:

Interface:

 

Note the following when defining:

1) abstract classes contain abstract methods. abstract keywords must be used for definition, and abstract classes cannot be instantiated.

2) abstract classes can contain non-Abstract Common methods and constructor methods. All methods in the interface must be abstract.

3) The access type of abstract methods in abstract classes can be public or protected. abstract METHODS In interfaces can only be public, and the default type is public abstract.

4) abstract classes can contain static methods. Interfaces cannot contain static methods.

My understanding:

1)Abstract classes can provide specific implementation of methods. When a subclass of an abstract class inherits the abstract class, these methods are obtained. interfaces can only provide method names, but cannot write specific implementations. If specific implementations are written, an error is reported when the class implementing the interface is compiled. In other words, the class implementing the interface must write the implementation of the Interface Method once.

2)Because of the single inheritance in C #, a class can only inherit one abstract class. A class as a type definition tool has limitations. interfaces are different, C # allows a class to implement multiple interfaces, and multiple interfaces can be of different types. Therefore, classes that implement multiple interfaces can have multiple types.

3)The interface separates "what to do" from "how to do", which means to understand coupling. Specifically, it solves the coupling between methods and classes of implementation methods, this enhances the maintainability of the program, which is a major benefit of the interface.

 

4)In simple terms, interfaces are public and used by others. Therefore, interfaces cannot have private attributes and Methods. abstract classes can have private attributes and methods.

5)Abstract classes are called abstract classes because there are abstract methods, and abstract methods must be modified by abstract keywords. Methods in interfaces do not need to be modified by keywords.

6)In the default adaptation mode, the interface is placed on the top layer and the abstract class is below. It can be combined with the advantages of the interface and the abstract class for ease of use.

 

Summary:Abstract classes and interfaces are similar to each other to a certain extent. In actual learning, we need to compare them to learn.

 

 

 

 

 

Related Article

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.