C # What are the benefits of abstract classes and interfaces of polymorphism ~~,

Source: Internet
Author: User

C # What are the benefits of abstract classes and interfaces of polymorphism ~~,

The similarities and differences between the abstract classes of polymorphism and interfaces may not be very complete. Learn from the experiences of watching videos and writing articles by yourself! For long review and use!

I. abstract classes

(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 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. abstract classes and interfaces
Similarities:
(1) can be inherited
(2) cannot be instantiated.
(3) All methods can contain method declarations.
(4) The derived class must implement the unimplemented method.
Partition:
(1) abstract base classes can define fields, attributes, and method declarations. The interface can only define attributes, indexers, events, and method declarations, and cannot contain fields.
(2) abstract classes are incomplete classes and need to be further refined. interfaces are a behavior specification. Microsoft's custom interface always carries the able field to prove that it is a class of "I Can Do It ~~"
(3) The interface can be implemented in multiple ways, and the abstract class can only be inherited by a single
(4) abstract classes are more defined in a series of closely related classes, while interfaces are mostly classes with loose relationships but all implement certain functions.
(5) abstract classes are abstract concepts from a series of related objects, so they reflect the internal commonalities of things. interfaces are a functional Convention defined to satisfy external calls, therefore, it reflects the external characteristics of things.
(6) The interface basically does not have any specific characteristics of inheritance. It only promises the methods that can be called.
(7) interfaces can be used to support callback, but inheritance does not have this feature
(8) The specific methods implemented by abstract classes are virtual by default, but the interface methods in the class implementing interfaces are non-virtual by default. Of course, you can also declare them as virtual
(9) If an abstract class implements an interface, you can map the methods in the interface to the abstract class, instead of implementing the methods in the abstract class.


If there are any mistakes or omissions, please reply and correct them!

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.