The difference between a C # interface and an abstract class

Source: Internet
Author: User

The abstract modifier is used to indicate that the class being decorated is incomplete, and that it can only be used as a base class. Abstract and non-abstract classes are different in the following ways:
Abstract classes cannot be instantiated directly, and new is used for abstract classes
Operator is a compile-time error. Although some variables and values at compile-time types can be abstract, such variables and values must either be
NULL, or contains a reference to an instance of a non-abstract class that derives from an abstract class.
An abstract class is allowed (but not required) to contain abstract members.
Abstract classes cannot be sealed.
When a non-abstract class is derived from an abstract class, these non-abstract classes must specifically implement all the abstract members that are inherited, overriding those abstract members. In the following example

Abstract class A
{
public abstract void F ();
}
Abstract class B:a
{
public void G () {}
}
Class C:b
{
public override void F () {
Actual implementation of F
}
}

Abstract class A introduces an abstract method F. Class B introduces another method, G, but since it does not provide an implementation of F, B must also be declared as an abstract class. Class C Overrides
F, and provide a concrete implementation. Because there are no abstract members in C, it is possible (but not necessary) to declare C as a non-abstract class.


Interface (interface)
Interfaces are reference types that are implemented by other types to ensure that they support certain operations. Interfaces are never created directly and have no actual representation, and other types must be converted to interface types.
An interface defines a contract. The class or struct that implements the interface must abide by its contract. An interface can contain methods, properties, indexers, and events as members.


The abstract modifier can be used with classes, methods, properties, indexers, and events.
Use the abstract modifier in a class declaration to indicate that the class can only be a base class for other classes.
Abstract classes have the following characteristics:
Abstract classes cannot be instantiated.
Abstract classes can contain abstract methods and abstract accessors.
You cannot modify an abstract class with the sealed modifier, which means that the class cannot be inherited.
A non-abstract class derived from an abstract class must include all inherited abstract methods and the actual implementation of the abstract accessor.
Use the abstract modifier in a method or property declaration to indicate that this method or property does not contain an implementation.
Abstract methods have the following characteristics:
An abstract method is an implicit virtual method.
Only abstract method declarations are allowed in abstract classes.
Because an abstract method declaration does not provide a real implementation, there is no method body; The method declaration ends with a semicolon, and there is no curly braces ({}) after the signature. For example:
public abstract void MyMethod (); implementations are provided by the overriding method, which is a member of a non-abstract class.
It is an error to use the static or virtual modifier in an abstract method declaration.
Abstract properties behave as abstract methods, except in the syntax of declarations and invocations.
It is an error to use the abstract modifier on a static property.
In a derived class, you can override an abstract inherited property by including a property declaration that uses the override modifier.
An abstract class must provide an implementation for all interface members.
An abstract class that implements an interface can map an interface method to an abstract method.

For example:

Interface I
{
void M ();
}
Abstract class C:i
{
public abstract void M ();
}

The difference between them:
1. A class is an abstraction of an object that can be interpreted as a class as an object, as an abstract class.
Interface is just a specification or rule of behavior, Microsoft's custom interface is always followed by a able field, proving that it is a class of expressions "I can do ... ”
Abstract classes are more defined in a series of tightly related classes, while interfaces are mostly loosely linked classes that 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 class implements a principle in oop, separating mutable from immutable. Abstract classes and interfaces are defined as immutable, and the variable seating subclass is implemented.

9 Good interface definitions should be functional, not multi-functional, otherwise causing 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.

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 a method in the subclass of an abstract class to implement an interface
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. If you create a feature that will be used across a wide range of heterogeneous objects, the interface is used. If you want to design a small and concise function block, use the interface. If you are designing large functional units, use an abstract class.
Use an abstract class if you want to provide common, implemented functionality across all implementations of a component.

This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/soialy/archive/2009/02/18/3906666.aspx

The difference between a C # interface and an abstract class

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.