C # differences between interfaces and abstract classes

Source: Internet
Author: User
C # differences between interfaces and abstract classes

Abstract modifier is used to indicate that the modified class is incomplete and can only be used as the base class. Abstract classes and non-abstract classes are different in the following aspects:
Abstract classes cannot be directly instantiated, and new is used for abstract classes.
The operator is a compilation error. Although some types of variables and values during compilation can be abstract, such variables and values must or
Null, or a reference to an instance that is not an abstract class (this non-abstract class is derived from the abstract class ).
Allowed (but not required) abstract classes include 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 inherited to override those abstract members. In the following example

 

Abstract Class A
{
Public Abstract Void F ();
}
Abstract Class B:
{
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 because it does not provide the Implementation of F, B must also be declared as an abstract class. Class C Rewriting
F, and provides a specific implementation. Since C does not have abstract members, you can (but not must) Declare C as a non-abstract class.

Interface)
Interfaces are reference types implemented by other types to ensure they support certain operations. Interfaces are never directly created and have no actual representation. Other types must be converted to interface types.
An interface defines a protocol. Classes or structures that implement interfaces must comply with their agreements. The interface can contain methods, attributes, indexers, and events as members.

Abstract modifiers can be used with classes, methods, attributes, indexers, and events.
Use the abstract modifier in the class declaration to indicate that the class can only be the base class of other classes.
Abstract classes have the following features:
Abstract classes cannot be instantiated.
Abstract classes can contain abstract methods and abstract accessors.
The abstract class cannot be modified with the sealed modifier, which means the class cannot be inherited.
A non-abstract class derived from an abstract class must include all the inherited abstract methods and the actual implementation of the abstract accessors.
Use the abstract modifier in a method or attribute declaration to indicate that this method or attribute does not contain an implementation.
Abstract methods have the following features:
The abstract method is an implicit virtual method.
Only abstract methods can be declared in abstract classes.
Because the abstract method declaration does not provide real implementation, there is no method body. The method declaration ends with a semicolon and there is no braces ({}) after the signature ({}). For example:
Public abstract void mymethod (); implemented by the overriding method, which is a non-abstract class member.
It is incorrect to use the static or virtual modifier in the abstract method declaration.
Except for the differences in the declaration and call syntax, abstract attributes behave the same way as abstract methods.
It is incorrect to use the abstract modifier for static attributes.
In a derived class, you can override abstract inheritance attributes by including the attribute declaration using the override modifier.
Abstract classes must be implemented for all interface members.
Abstract classes that implement interfaces can map interface methods to abstract methods.

For example:

 

Interface I
{
Void M ();
}
Abstract Class C: I
{
Public Abstract Void M ();
}

 

Differences between them:
1. Classes are abstract objects. abstract classes can be interpreted as classes as objects and abstracted into classes.
An interface is only a specification or provision of behavior. Microsoft's custom interface always carries the able field to prove that it represents a class of "I can do it ..."
Abstract classes are more defined in a series of closely related classes, while interfaces are mostly classes with loose relationships but all implement certain functions.

2. The interface basically does not have any specific characteristics of inheritance. It only promises the methods that can be called;

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

4. interfaces can be used to support callback, but inheritance does not.

5. the abstract class cannot be sealed.

6. The specific methods implemented by abstract classes are virtual by default, but the interface methods in the class implementing interfaces are non-virtual by default. Of course, you can also declare them virtual.

7. Similar to a non-abstract class, an abstract class must provide its own implementation for all the members of the interface listed in the base class list of this class. However, the abstract class is allowed to map interface methods to abstract methods.

8 abstract classes implement a principle in OOP to separate mutable and immutable. Abstract classes and interfaces are defined as immutable classes, while variable class classes are implemented.

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

10 avoid using inheritance to implement the build function, but use black box multiplexing, that is, object combination. As the hierarchy of inheritance increases, the most direct consequence is that when you call a class in this group, you must load all of them into the stack! The consequences can be imagined.

11 if an abstract class implements an interface, you can map the methods in the interface to an abstract class without having to implement them. In the subclass of the abstract class, you can implement methods in the interface.
If you want to create multiple versions of a component, create an abstract class. Abstract classes provide simple methods to control component versions. If the created function is used across a wide range of different objects, the interface is used. If you want to design small and concise functional blocks, use interfaces. If you want to design a large functional unit, use an abstract class.
If you want to provide common implemented functions among all the implementations of a component, use an abstract class.

 

 

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

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.