The difference between an interface and an abstract class in C #

Source: Internet
Author: User

  Interfaces and abstract classes are the points that are frequently detected in an interview and are easily confused. First understand the concept of the two:

First, abstract class:
Abstract classes are special classes that cannot be instantiated, except that they have other characteristics of classes, and it is important that abstract classes include abstract methods, which are 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 overwrite them. In addition, abstract classes can derive from an abstract class that can override the base class's abstract methods or not, and if not overwritten, their derived classes must overwrite them.

Second, the interface:

Interfaces are reference types, similar to classes, and are similar to abstract classes with three points:

1, can not be instantiated;

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

3. Derived classes must implement methods that are not implemented, abstract classes are abstract methods, and interfaces are all members (not just methods including other members);

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 only a single class (including abstract classes) is inherited directly.

Detailed, definition of the difference analysis, you can refer to this blog: http://www.cnblogs.com/lovemyth/archive/2008/09/08/828909.html

  Here is an overview and a visual explanation of the differences between interfaces and abstract classes:

Interfaces are primarily abstractions of behavior, and abstract classes are primarily abstractions of classes.

  A more figurative metaphor is as follows:

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)).

  The difference in code level shows:

First, abstract class:

Abstract classes can include some concrete implementations, while interfaces are not.

  

 Public Abstract classaclass{Private intx; Private inty;  Public intXGet{returnx;} }     Public intYGet{returny;} }      Public floatSum () {returnX +Y;}  Public Abstract floatAverage ();}  Public classbclass:aclass{ Public Override floatAverage () {returnSum ()/2; }}

As the above example, in the abstract class, the actual includes: several specific implementations, two attributes, a summation method.
At the same time, for the interface, the field member cannot be defined at all, that is, the private int x,private int y;

Second, the interface:

For an interface to achieve the same functionality, you can only do:

 Public Interface a{    publicint x{get;}      Public int y{get;}      Public float Sum ();}

You cannot provide any function of sum.

  Summary:

1, we understand a technology, the first can be considered from its usefulness, to recognize that the technology is what to do with. This will deepen the understanding of knowledge. The interfaces and abstract classes used in subsequent projects will continue to refine this document.

2, the article is only their own summary and experience (including paste), hope to provide you with help. Thanks to the csdn PHY, and the Cnblog user remnant leaves.

  

The difference between an interface and an abstract class in C #

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.