Interfaces and abstract classes in C #

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:
usingSystem; Public Abstract classA//Abstract Class A{     Private intnum=0;  Public intNum//abstract class contains properties    {         Get         {             returnnum; }         Set{num=value; }             }     Public Virtual intGetnum ()//abstract class contains virtual methods    {         returnnum; }     Public voidSetnum (intN// //abstract classes contain common methods    {          This. num =N; }     Public Abstract voidE ();//abstract method in Class A E    } Public Abstract classB:a//because Class B inherits the abstract method E from Class A, Class B also becomes an abstract class{     } Public classc:b { Public Override voidE ()//overrides an 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 classTest {Static voidMain () {c C=NewC ();     C.E (); } }
Second, the interface

(1) interface cannot be instantiated
(2) The interface can only contain method declarations, cannot contain common methods, etc.
(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 voidEventHandler (Objectsender, Event e); Public InterfaceITest {//int x = 0;    intA {Get; Set; }    voidTest (); EventEventHandler Event; int  This[intIndex] {         Get; Set; } }
    (5) All members in the interface are public by default, so the private modifier cannot be in the interface (typically, no modifiers are added to the interface)
(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

(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

Interfaces and abstract classes in C #

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.