C # What is the difference between interfaces and abstract classes in the object-oriented programming of Zhixin learning series? (18th)

Source: Internet
Author: User

  Reading directory
I. Preface

Ii. What is the difference between interfaces and abstract classes?

  I. Preface
In object-oriented programming, abstraction is an agreement that the successor or implementer must follow. For example, if a class inherits or implements an interface, the class must implement all the members of the interface, an interface is an abstraction of A Class. A Class must follow the protocol of all its members.

  Ii. What is the difference between interfaces and abstract classes?
Interfaces and abstract types are two mechanisms used to implement abstraction in Object-Oriented Programming. So when can I use interfaces to implement abstraction in object-oriented programming? Under what circumstances can abstract be implemented using abstract types?
1: The interface cannot provide implementation details of members, but the abstract type can provide implementation details of members. That is to say, all methods in the interface must be abstract, as shown in the following InserUser () and IGetName (), and the access modifier of the method cannot be specified, but there can be method implementation in the abstract type. For example, the following GetName () can also specify the access modifier public, if you need to provide implementation details for Members during design, select the abstract type.
Interface
Interface Interface1
{
Void IInserUser ();
String IGetName ();
}
Abstract type
Public abstract class Class1
{
Public abstract void InserUser ();

Public virtual string GetName ()
{
If (2 = 2)
Return "getting sleepy from an early age ";
}
}
2: in C #, classes and classes are single-inherited. A class can only inherit from one abstract class, while a class can inherit multiple interfaces, if you want to implement multi-inheritance during design, select the interface

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.