The difference between C # abstract classes and interfaces

Source: Internet
Author: User

Abstract class: Abstract class is a special class, but cannot be instantiated (you can instantiate a base class object with a derived class); In addition, there are other attributes of the class; It is important that abstract classes can include abstract methods (of course, it can have normal methods), which is not available to ordinary classes. Abstract methods can only be declared in an abstract class and do not contain any implementations, and derived classes must override (overwrite) them.
Interface: interface contains a prototype of a set of methods, the interface is a reference type, similar to the class, and the similarity of the abstract class has three points:

1, can not be instantiated;

2, including the non-implementation of the method declaration;

3, the derived class must implement the method of the interface;

In addition, interfaces have the following characteristics: Interfaces can contain properties, indexers, events, and those members are defined as public, in addition to methods. In addition, you cannot include any other members, such as constants, fields, constructors, destructors, static members. A class can inherit directly from multiple interfaces, but can inherit only one class directly (including abstract classes)

Third, the difference between abstract class and interface:

1. A class is an abstraction of an object, which can be interpreted as "treating a class as an object, and then abstracting a class as an abstract class", whereas an interface is just a specification or a rule of behavior, and Microsoft's custom interface is always followed by a able field, proving that it is a class "I can do ... ". Abstract classes are more defined in a series of tightly related classes, whereas interfaces are mostly in classes where relationships are loose but all implement a function.

2. The interface basically does not have any specific characteristics of inheritance, it only promises to be able to invoke the method;

3. A class can implement several interfaces at a time, but can only extend one parent class

4. Interfaces can be used to support callbacks, and inheritance does not have this feature.

5. Abstract classes cannot be sealed.

6. The concrete method implemented by the abstract class defaults to virtual, but the interface method in the class that implements the interface defaults to non-virtual, and of course you can declare it as virtual.

7. (interfaces) similar to non-abstract classes, an abstract class must also provide its own implementation for all members of the interface listed in the base class list of the class. However, an abstract class is allowed to map an interface method to an abstract method.

8. Abstract classes implement a principle in oop, separating mutable from immutable. Abstract classes and interfaces are defined as immutable, and the variable seating subclass is implemented.

9. A good interface definition should be functional, not multi-functional, or cause interface pollution. If a class just implements one of the functions of this interface, and has to implement other methods in the interface, it is called interface pollution.

10. Try to avoid using inheritance to implement the build function, but instead use black box multiplexing, which is the object combination. Because of the increasing level of inheritance, the most immediate consequence is that when you call one of these classes, you have to load them all into the stack! The consequences are conceivable. (in conjunction with the stack principle). At the same time, the heart of friends can notice that Microsoft in the construction of a class, many times the use of the object combination method. For example, in ASP. NET, the page class has properties such as server request, but in fact they are all objects of a class. Using this object of the page class to invoke the methods and properties of another class is a very basic design principle.

11. If an abstract class implements an interface, it is possible to map a method in an interface to an abstract class as an abstract method without having to implement the method in the subclass of the abstract class.

Iv. use of abstract classes and interfaces:

1. If you expect to create multiple versions of a component, create an abstract class. Abstract classes provide an easy way to control the component version.

2. If you create a feature that will be used across a wide range of heterogeneous objects, use an interface. If you want to design a small and concise function block, use the interface.

3. If you are designing large functional units, use an abstract class. Abstract classes are used if you want to provide common, implemented functionality across all implementations of a component.

4. Abstract classes are primarily used for closely related objects, while interfaces are suitable for providing common functionality for unrelated classes.

Here are some of the image metaphors I've seen online:

1. Aircraft will fly, birds will fly, they all inherit the same interface "Fly"; but F22 belongs to the abstract class of airplanes, the pigeons belong to the abstract class of birds.

2. Like iron doors are doors (abstract class), you want a door I can't give (can't instantiate), but I can give you a concrete iron gate or wooden door (polymorphic); and only the door, you can't say it is a window (single inheritance); A door can have a lock (interface) can also have a doorbell (multi-implementation). The door (abstract class) defines what you are, and the interface (lock) specifies what you can do (an interface is best to do only one thing, and you cannot ask for a lock to make a sound (interface pollution)).

==========================================================

Abstract classes in C # are somewhat similar to interfaces, and beginners are easy to confuse, so let me talk about their understanding today. First we need to clarify the meaning of the two, as follows: If a class is not associated with a specific thing, but simply expresses an abstract concept, just as a base class of its derived class, such a class is abstract class, which can provide both abstract methods and non-abstract methods. Abstract classes cannot be instantiated, and they must be implemented by derived classes to implement their abstract methods, so the new keyword cannot be used for abstract classes and cannot be sealed. If a derived class does not implement all of the abstract methods, the derived class must also be declared as an abstract class. In addition, implementing an abstract method is implemented by the Override keyword. Interface (interface) is used to define a protocol for a program. It is an abstract type that contains a set of virtual methods, each of which has its name, parameters, and return values. An interface method cannot contain any implementation, and the CLR allows an interface to contain events, properties, indexers, static methods, static fields, static constructors, and constants. A class can implement multiple interfaces, and when a class inherits an interface, it not only implements all the methods defined by that interface, but also implements all the methods that the interface inherits from the other interfaces.

Abstract class is the same point as the interface:

1. All can be inherited.

2. Neither can be instantiated.

3. You can include a method declaration.

4. Derived classes must implement methods that are not implemented.

The different points of an abstract class from an interface:

1. The interface supports multiple inheritance, and abstract classes cannot implement multiple inheritance (single-sex). That is, a class can implement several interfaces at a time, but only one parent class is inherited.

2. Interfaces only contain signatures for methods, properties, indexers, events, but cannot define fields and methods that contain implementations; Abstract classes can define fields, properties, and methods that contain implementations.

3. An interface is a set of behavior specifications; An abstract class is an incomplete class that focuses on the concept of ethnicity.

4. Interfaces can be used to support callbacks, and abstract classes cannot implement callbacks because inheritance is not supported.

5. Interfaces can be used for value types and reference types, and abstract classes can only be used for reference types. For example, a struct can inherit an interface and not inherit a class.

6. Interface multi-definition object behavior, focusing on can-do relationship type, abstract class multi-definition object properties, emphasis on is-a-type relationship.

7. The concrete method of the abstract class implementation is virtual by default, but the interface method in the class that implements the interface defaults to non-virtual and of course it can be declared as virtual.

Is it a coincidence that there are so many similarities between interfaces and abstract classes? No, in fact, from the Anti-compilation tool can see the interface is an abstract class, abstract can not be instantiated, can only be implemented. An interface is a class that indicates that it is also a data type that can create an interface object through an interface (the so-called interface object is actually the object of the class that implements the interface), which is the nature of the interface. Knowing the difference between an abstract class and an interface, and understanding the nature of the interface, how better to use them? Simply put, abstract classes are primarily used for closely related objects, and interfaces are used to provide common functionality for unrelated classes. I make a popular analogy, people will eat, the dog will eat, they all inherit the same interface "eat", but the programmer belongs to the human, the lion dog belongs to the dog, the person and the dog is different species, is irrelevant type, but because realizes the same interface "eats" thus will eat. A programmer is a person, a Poodle is a dog, and a subclass is closely related to its parent so that it can be reused by inheriting the original attribute. Give you the concept of "human" you can't know what kind of person, give you a "dog" concept you can not know what the dog, in here people and dogs are abstract class concept, because it is abstract, so can not be instantiated, only through subclass inheritance, the implementation of the parent class is not implemented method, can be instantiated, Just know that this person is a programmer, the dog is a poodle.

The difference between C # abstract classes and interfaces

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.