What is the difference between abstract classes and interfaces in C #. Net?

Source: Internet
Author: User
What is the difference between abstract classes and interfaces in C #. Net?

What is the difference between abstract classes and interfaces in C #. Net?

Best Answer:
1 abstract class
(1) abstract methods are declared only, but do not contain implementations. They can be considered as virtual methods without implementations.
(2) abstract classes cannot be instantiated.
(3) abstract classes can but do not have to have abstract attributes and abstract methods. However, once an abstract method is available, you must declare this class as an abstract class.
(4) The specific derived class must overwrite the abstract method of the base class.
(5) An abstract derived class can overwrite or overwrite the abstract methods of the base class. If not overwritten, the specific derived class must overwrite them. For example:

Using system;
Public abstract class A // abstract class
{
Private int num = 0;
Public int num // abstract class containing attributes
{
Get
{
Return num;
}
Set
{
Num = value;
}
}

Public Virtual int getnum () // abstract class contains Virtual Methods
{
Return num;
}

Public void setnum (int n) // The abstract class contains common methods.
{
This. num = N;
}

Public abstract void E (); // abstract method E in Class
}

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

}
Public Class C: B
{
Public override void E () // override the abstract method inherited from Class. If Class B also defines an abstract method, it must be rewritten.
{
// Throw new exception ("the method or operation is not implemented .");
}
}

Public class test
{
Static void main ()
{
C = new C ();
C. E ();
}
}

2. Access Port
(1) The interface cannot be instantiated.
(2) interfaces can only contain method declarations.
(3) interface members include methods, attributes, indexers, and events.
(4) The interface cannot contain constants, fields (fields), constructors, destructor, and static members. For example:
Public Delegate void eventhandler (Object sender, event E );
Public interface itest
{
// Int x = 0;
Int
{
Get;
Set;
}
Void test ();
Event eventhandler event;
Int this [int Index]
{
Get;
Set;
}
}
(5) All the members in the interface are public by default, so the interface cannot have a private Modifier
(6) The derived class must implement all the members of the interface.
(7) A class can directly implement multiple interfaces separated by commas.
(8) An interface can have multiple parent interfaces. The class implementing this interface must implement all the members of all parent interfaces.

III. Similarities between abstract classes and interfaces
(1) can be inherited
(2) cannot be instantiated
(3) can contain method declaration
(4) A derived class must implement an unimplemented method
partition:
(1) abstract base classes can define fields, attributes, and methods. The interface can only define attributes, indexers, events, and method declarations, and cannot contain fields.
(2) an abstract class is an incomplete class and needs to be further refined. The interface is a behavior specification. Microsoft's custom interface always carries the able field to prove that it is a class of expression "I can do it ..."
(3) interfaces can be implemented in multiple ways, and abstract classes can only be inherited in a single way.
(4) abstract classes are defined in a series of closely related classes, in most of the classes whose interfaces are loose but all implement a certain function,
(5) abstract classes are abstract concepts from a series of related objects, therefore, it reflects the internal commonality of things. interfaces are a functional Convention defined to satisfy external calls, so they reflect the external characteristics of things
(6) basically, an interface does not have any specific characteristics of inheritance. It only promises methods that can be called.
(7) an interface can be used to support callback, inheritance does not have this feature.
(8) the specific methods implemented by the abstract class are virtual by default, but the interface methods in the class implementing the interface are non-virtual by default, of course, you can also declare it as virtual
(9) If an abstract class implements an interface, you can map the methods in the interface to the abstract class as an abstract method without having to implement it, implement Methods in the interface in the subclass of the abstract class

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.