My understanding of abstract classes and interfaces in C #

Source: Internet
Author: User

My understanding of abstract classes and interfaces in C #
Abstract classes in C # are similar to interfaces, which are easy to confuse for beginners. Let's talk about their understanding today. First, we need to clarify the meanings of the two. If a class is not associated with a specific thing, it only expresses an abstract concept and serves as a base class of its derived class, this class is an abstract class. It can provide both abstract methods and non-abstract methods. Abstract classes cannot be instantiated. They must be inherited by the derived classes to implement their abstract methods. Therefore, abstract classes cannot use the new keyword or be sealed. If the derived class does not implement all abstract methods, the derived class must also be declared as an abstract class. In addition, the abstract method is implemented by the override keyword. An interface is a protocol used to define a program. It is an abstract type that contains a set of virtual methods. Each method has its name, parameter, and return value. Interface methods cannot contain any implementations. CLR allows interfaces to include events, attributes, indexers, static methods, static fields, static constructors, and constants. A class can implement multiple interfaces. When a class inherits an interface, it must not only implement all methods defined by this interface, but also implement all methods inherited from other interfaces. Similarities between abstract classes and interfaces: 1. Both can be inherited. 2. They cannot be instantiated. 3. All methods can contain method declarations. 4. The derived class must implement the unimplemented method. Differences between abstract classes and interfaces: 1. interfaces support multi-inheritance; abstract classes cannot implement multi-inheritance (single-root ). That is, a class can implement several interfaces at a time, but can inherit only one parent class. 2. interfaces only contain methods, attributes, indexers, and event signatures, but cannot define fields or methods that contain implementations. abstract classes can define fields, attributes, and methods that have implementations. 3. interfaces are a set of behavioral norms. abstract classes are incomplete classes that focus on the concept of ethnic groups. 4. interfaces can be used to support callback. abstract classes cannot implement callback because inheritance is not supported. 5. The interface can act on the Value Type and reference type. The abstract class can only act on the reference type. For example, Struct can inherit interfaces rather than classes. 6. Interface multi-definition object behavior, focusing on CAN-DO relational type; abstract class multi-definition object attributes, focusing on the relationship of IS-A. 7. The specific methods implemented by abstract classes are virtual by default. The interface methods in the class implementing interfaces are non-virtual by default. Of course, they can also be declared as virtual. Is it a coincidence that interfaces and abstract classes have such a multi-phase similarity? No. In fact, The Decompilation tool shows that the interface is an abstract class. Abstract objects cannot be instantiated and can only be implemented. An interface is a class, indicating that it is also a data type. You can create an interface object through the interface (the so-called interface object is actually an object of the class that implements this interface ), this is the essence of interfaces. I know the differences between abstract classes and interfaces and the essence of interfaces. How can I use them better? In short, abstract classes are mainly used for closely related objects, and interfaces are used to provide general functions for irrelevant classes. For example, people eat, and dogs eat. They all inherit the same interface "eat", but programmers belong to abstract classes, and dogs belong to dogs, humans and dogs are different species and are irrelevant, but they all eat because they implement the same interface "eat. Programmers are human beings and dogs are dogs. sub-classes are closely related to their parent classes, so they can inherit and reuse the original attributes. Give you a concept of "man". You cannot know what kind of person you are. Give you a concept of "dog" and you cannot know what kind of dog you are, here, humans and dogs are the concept of abstract classes. Because they are abstract classes, they cannot be instantiated. They can be instantiated only when a method not implemented by the parent class is inherited by sub-classes, that's why we know that this guy is a programmer and that dog is a pub dog. Of course, understanding is only a kind of understanding of its nature. There are still many things to be paid attention to in programming practices. This is still to be learned and we hope to continue to improve with readers.

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.