C#. What is the difference between an abstract class and an interface in net?

Source: Internet
Author: User

1 abstract class

(1) Abstract methods are declared only, not implementations, and can be seen as virtual methods without implementing the body.
(2) Abstract classes cannot be instantiated
(3) Abstract classes can, but do not have to have abstract attributes and abstract methods, but once you have an abstract method, you must declare this class as an abstract class
(4) The specific derived class must override the abstract method of the base class
(5) An abstract derived class can override an abstract method of a base class, or it can be overridden. If they are not overwritten, their specific derived classes must overwrite them. Such as:

Using System;

public abstract Class A//abstract classes A
{
private int num=0;

public int Num//abstract class contains attributes
{
Get
{
return num;
}
Set
{
num = value;
}

}

public virtual int Getnum ()//abstract class contains virtual methods
{
return num;
}

public void Setnum (int n)/////abstract class contains common methods
{
This.num = n;
}

public abstract void E (); Abstract method in Class A E

}

Public abstract class B:A//Because Class B inherits the abstract method E from class A, Class B also becomes an abstract class
{

}

public class C:b
{
public override void E ()//overrides the abstract method inherited from Class A. If Class B itself also defines an abstract method, you must also override the
{
throw new Exception ("The method or operation is not implemented.");
}
}

public class Test
{
static void Main ()
{
c C = new C ();
C.E ();
}
}

Second, the interface

(1) interface cannot be instantiated
(2) The interface can only contain method declarations
(3) The members of an interface include methods, properties, indexers, events
(4) The interface cannot contain constants, fields (domains), constructors, destructors, static members. Such as:

public delegate void EventHandler (object sender, Event e);

public interface ITest
{
int x = 0;

int A
{
Get
Set
}

void Test ();

Event EventHandler event;

int This[int index]
{
Get

Set
}
}

(5) All members in the interface default to public, so the interface cannot have the private modifier
(6) Derived classes must implement all members of an interface
(7) A class can directly implement a plurality of interfaces, the interface is separated by commas
(8) An interface can have multiple parent interfaces, and the class implementing the interface must implement all the members of all parent interfaces

III. abstract classes and interfaces
Same point:
(1) Can be inherited
(2) can not be instantiated
(3) can contain method declarations
(4) A derived class must implement a method that is not implemented
Difference
(1) Abstract base classes can define fields, properties, method implementations. An interface can only define properties, indexers, events, and method declarations, and cannot contain fields.
(2) An abstract class is an incomplete class that needs further refinement, while an interface is a code of conduct. Microsoft's custom interface is always behind the able field, proving that it is stating a class of "I can do ... ”
(3) Interfaces can be multiple implementations, abstract classes can only be inherited by a single
(4) The abstract class is more defined in a series of closely related classes, and the interface is mostly loose but all of the classes that implement a function
(5) Abstract class is the concept of abstraction from a series of related objects, so it reflects the internal commonality of things; an interface is a functional contract defined to satisfy an external invocation, so it reflects the external nature of the thing.
(6) The interface basically does not have any specific characteristics of inheritance, it only promises to be able to invoke the method
(7) interfaces can be used to support callbacks, and inheritance does not have this feature
(8) The concrete method of the abstract class implementation is virtual by default, but the interface method in the class that implements the interface defaults to non-virtual, and of course you can declare it as virtual.
(9) If an abstract class implements an interface, it is possible to map a method in an interface to an abstract class as an abstract method without having to implement a method in the subclass of an abstract class to implement an interface

C#. What is the difference between an abstract class and an interface in net?

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.