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

Source: Internet
Author: User

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

The concepts of interfaces and abstract classes are different. An interface is an abstraction of an action, and an abstract class is an abstraction of the root.

The abstract class represents what this object is. The interface represents what this object can do. For example, men, women, these two classes (if it's a class ...) ), their abstract class is human. Description, they are all human.

People can eat, dogs can eat, you can define "eat" as an interface, and then let these classes to implement it.

Therefore, in high-level languages, a class can inherit only one class (abstract class) (just as people cannot be both biological and non-living), but can implement multiple interfaces (eating interfaces, walking interfaces).

Let's talk about the difference between the two applications :
interface is more in the system architecture design method to play a role, mainly used to define the communication contract between the modules.  
Abstract classes play a role in code implementation and can be reused for code

Template method Design pattern is a typical application of abstract class

For example

The template method design pattern is a typical application of an abstract class, assuming that all servlet classes of an item will use the same method for permission judgments, log access logs, and handle exceptions.

You can then define an abstract base class that all servlets inherit from this abstract base class, complete the permission judgments, log access logs, and code that handles exceptions in the service method of the abstract base class.

Best Answer:
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

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.