Similarities and differences between abstract classes and interfaces, and between abstract class interfaces

Source: Internet
Author: User
Tags mail exchange

Similarities and differences between abstract classes and interfaces, and between abstract class interfaces

This article summarizes some similarities and differences between interfaces and abstract classes in C # to share with you.

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.

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.

(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) 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 methods. 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 expression "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.

 

Author: xin, published in blog

Reprinted please indicate the source, welcome to mail exchange: zhuanxinxin@foxmail.com

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.