Differences between interfaces and abstract classes in C,

Source: Internet
Author: User

Differences between interfaces and abstract classes in C,

  Interfaces and abstract classes are often obtained during interviews and are easy to confuse. First, understand the concepts of the two:

I. abstract class:
Abstract classes are special classes, but they cannot be instantiated. In addition, they have other characteristics of the class. It is important that abstract classes can include abstract methods, which are not supported by common classes. Abstract METHODS can only be declared in abstract classes and do not contain any implementations. The Derived classes must overwrite them. In addition, an abstract class can be derived from an abstract class. It can overwrite the abstract methods of the base class or overwrite them. If not, its derived class must overwrite them.

Ii. interface:

The interface is of reference type. It is similar to a class and has three similarities with the abstract class:

1. It cannot be instantiated;

2. contains an unimplemented method statement;

3. The derived class must implement unimplemented methods. The abstract class is an abstract method, and the interface is all members (not only the methods include other members );

In addition, interfaces have the following features: In addition to methods, interfaces can also contain attributes, indexers, and events, and these members are defined as common. It cannot contain any other Members, such as constants, fields, constructors, destructor, and static members. A class can directly inherit multiple interfaces, but can only inherit one class (including abstract classes ).

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

  Here, the differences between interfaces and abstract classes are explained in a general and visualized way:

The interface is mainly an abstraction of behaviors; the abstract class is mainly an abstraction of classes.

  A better image is as follows:

1. Planes fly and birds fly. They all inherit the same interface "fly". However, F22 belongs to the aircraft abstract class and pigeon belongs to the bird abstract class.
2. just like all doors (abstract class), I can't give you a door (I can't instantiate it), but I can give you a specific door or wooden door (polymorphism ); it can only be a door. You cannot say it is a window (single inheritance); a door can have a lock (Interface) or a doorbell (multiple implementations ). A door (abstract class) defines what you are and an interface (LOCK) specifies what you can do (one interface is best to do only one thing, you cannot require the lock to make sound (interface pollution )).

  Code-level differences:

I. abstract class:

Abstract classes can include some specific implementations, but interfaces cannot.

  

public abstract class AClass{    private int x;    private int y;    public int X { get { return x; } }    public int Y { get { return y; } }     public float Sum() { return X + Y; }     public abstract float Average();} public class BClass : AClass{    public override float Average()    {        return Sum() / 2;    }}

In the example above, the abstract class actually includes the following specific implementations, two attributes, and a summation method.
At the same time, for the interface, it is impossible to define the field members, that is, private int x and private int y; it cannot be defined.

Ii. interface:

To implement the same functions on interfaces, you can only:

public interface A{    public int X{get;}    public int Y{get;}    public float Sum();}

You cannot provide any Sum function.

 

  Summary:

1. In terms of understanding a technology, we can first consider it from its usage, and realize what the technology is. In this way, we can deepen our understanding of knowledge. Interfaces and abstract classes used in subsequent projects will continue to improve this document.

2. The article is just my summary and experience (including pasting), hoping to help you. Thanks to CSDN's phy and CNBLOG users.

 

  

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.