Differences between abstract classes and interfaces in C #

Source: Internet
Author: User
Tags define abstract

Differences between abstract classes and interfaces in C #

The importance of interfaces in C # describes how to declare and use interfaces.

But C # is an abstract class formed by abstract keywords. How should we choose between them!

C # declare classes and functions as abstract objects. Abstract classes cannot be instantiated, and abstract functions cannot be directly implemented. They must be rewritten in non-Abstract Derived classes. Obviously, abstract functions are virtual, but virtual keywords cannot be provided. If a class contains an abstract function, the class is also abstract and must be declared as abstract.

Define abstract classes:

public abstract class Animal{    protected string _name;    public abstract string Name    {        get;    }    public abstract void Show();    public void MakeVoice()    {        Console.WriteLine(All animals can make voice!);    }}

Interface Definition:

public interface IAction{    void Move();}

Implement abstract classes and interfaces:

public class Duck : Animal, IAction{    public Duck(string name)    {        _name = name;    }    public override void Show()    {        Console.WriteLine(_name + is showing for you!);    }    public override string Name    {        get{return _name;}    }    public void Move()    {        Console.WriteLine(Duck also can swim.);    }}public class Dog : Animal, IAction{    public Dog(string name)    {        _name = name;    }    public override void Show()    {        Console.WriteLine(_name + is showing for you!);    }    public override string Name    {        get{return _name;}    }    public void Move()    {        Console.WriteLine(_name + Duck also can run.);    }}

Client implementation:

public class TestAnimal{    public static void Main(string [] args)    {    Animal duck = new Duck(Duck);    duck.MakeVoice();    duck.Show();    Animal dog = new dog(Dog);    dog.MakeVoice;    dog.Show();    IAction dogAction = new Dog(A big dog);    dogAction.Move();    }}

1. abstract classes are used to extract the common expression of a specific class, and interfaces are used to aggregate irrelevant classes into a common group.

2. All codes are shared and scalable. Abstract Class should be used whenever possible.

3. Use interfaces when looking for functional commonalities between objects with large differences. Use abstract classes when looking for functional differences between objects with large differences.

4. If you want to create multiple versions of a component, we recommend that you use an abstract class. If you design small and concise functional blocks, use interfaces.

 

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.