The difference between C # interfaces and abstract classes

Source: Internet
Author: User
Tags modifier

Abstraction Class (abstract class):
The abstract modifier is used to indicate that the decorated class is incomplete and that it can only be used as a base class. Abstract classes are different from Non-abstract classes in the following ways:
Abstract classes cannot be instantiated directly, and the abstract class is used with the new
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 (this non-abstract class derives from an abstract class).
Allow (but not require) abstract classes to contain abstract members.
Abstract classes cannot be sealed.
When deriving non-abstract classes from an abstract class, these non-abstract classes must specifically implement all of the inherited abstract members, 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 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 Override
F, and provide a concrete implementation. Because there are no abstract members in C, you can, but do not have 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 there are no actual representations, and other types must be converted to interface types.
An interface defines a contract. A class or struct that implements an interface must comply with its contract. Interfaces 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 a class can only be a base class for other classes.
An abstract class has the following attributes:
An abstract class cannot be instantiated.
Abstract classes can contain abstract methods and abstract accessors.
An abstract class cannot be modified with the sealed modifier, which means that the class cannot be inherited.
A non-abstract class that derives from an abstract class must include the actual implementation of all inherited abstract methods and abstract accessors.
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.
Abstract method declarations are allowed only in abstract classes.
Because an abstract method declaration does not provide a real implementation, there is no method body; The method declaration ends only with a semicolon, and there is no curly brace ({}) after the signature. For example:
public abstract void MyMethod (); Implementation is provided by the overriding method, which is a member of a Non-abstract class.
It is an error to use a static or virtual modifier in an abstract method declaration.
Abstract properties behave like abstract methods, except for differences in declaration and invocation syntax.
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 implementations 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, which can be interpreted as a class as an object, as an abstract class
Interface is just a behavior of the specification or regulation, Microsoft's custom interface is always followed by the able field, proving that it is to express a class of classes "I can do ... ”
Abstract classes are more defined in a series of tightly related classes, while interfaces are mostly loose but implement a functional class
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 of the abstract class implementation defaults to virtual, but the interface method in the class that implements the interface is not virtual by default, but you can also declare it as virtual.

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

The 8 abstract class implements a principle in oop that separates mutable from immutable. Abstract classes and interfaces are defined as immutable, and the variable seating subclasses are implemented.

9 Good interface definition should be a specific functional, rather than multi-functional, otherwise cause interface pollution. If a class only implements one of the functions of the interface and has to implement other methods in the interface, it is called interface pollution.

10 avoid the use of inheritance to achieve the build function, but the use of black box reuse, that is, the combination of objects. Because the level of inheritance increases, the most immediate consequence is that when you call a class in this group, you must load them all into the stack. The consequences are conceivable.

11 If an abstract class implements an interface, you can map the methods in an interface to an abstract method in an abstract class without having to implement them, and implement the methods in an interface in a subclass of an abstract class
If you expect to create multiple versions of a component, you create an abstract class. Abstract classes provide a simple way to control the component version. If you create a feature that will be used across a wide range of objects, use an interface. If you want to design a small and concise feature block, use an interface. If you want to design a large functional unit, use an abstract class.
Use an abstract class if you want to provide common, implemented functionality between all implementations of a component.

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.