An explanation of abstract classes and interfaces in C #

Source: Internet
Author: User
The problem arises:

When we use the abstract classes and interfaces of C #, we often encounter the following similar problems, broadly summarized as follows:

(1) What are the essential differences and connections between abstract classes and interfaces?

(2) When to choose the use of abstract classes, then when to use the interface is most appropriate?

(3) How to use the project in order to make the project more maintainable, extensibility? How to combine it with the struct, the class tightly, achieves the final double-edged sword action?

Solution:

This is also the problem I encountered when learning abstract classes and interfaces, from the three questions I summed up, it is not difficult to see that this is perhaps the three stages that most of our programmers are experiencing problems,

The first stage (basic concept): Just like question 1, this part of the people need first to clear the basic concept of the barrier, first of all to understand what is called abstract class, what is called interface?

Then what is the difference between an abstract class and an interface and what is the connection? Of course, this may take a while to understand and practice, after all, these concepts are more abstract, belong to the kind of things that can not be seen, of course, the most important or more practice, nothing to do a demo instance, they are used again, in the process of using more think about why to use this? What is the benefit of this? Can you use interfaces, and if not, where are the benefits of using abstract classes? This can deepen the understanding of them, this is my little experience it, hehe! Said so much, I would like to summarize the question 1, one is to facilitate their own mind, the second is to deepen understanding it.

Abstract class and interface concepts: In fact, these concepts in textbooks and blogs basically a large pile, the predecessors summed up is also very good, but may be in the popular, easy to understand a bit obscure, I will translate, add Shaanxi version of the vernacular, hey.

(1) Abstract class: Provides a set of derived classes to access shared base class public methods;

Abstract classes are characterized by: (1) Abstract classes include both abstract and method implementations, and (2) abstract classes cannot be instantiated or sealed; (3) abstract methods in abstract classes are either implemented in derived classes or inherited by derived abstract classes (abstract derived classes can inherit the base class abstract method). If you want to implement an abstract method of a base class in a derived class, you must use the override modifier; (4) The abstract class belongs to the single inheritance (this is the same-sex of all Classes) (5) Abstract class is an abstraction of an ethnic group, similar to is-a;

The above I if not very clear, give you an official website about abstract class address: Https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/abstract

(2) Interface: An abstract type containing a set of virtual methods;

The characteristics of the interface are: (1) Only the definition of the virtual method is included in the interface, only the declaration definition, no function implementation, (2) The interface class can include attributes, events, indexers, etc., but cannot include fields, (3) The interface class is multi-inheritance, (4) inherits the interface of the class must all implement the interface method

Abstract classes and interfaces differ and relate to:

The same point: (1) can not be directly instantiated, can only be achieved by means of inheritance;

(2) is the abstract of the behavior and objects of things, forming a certain pattern of design;

Different points:

(1) The interface supports multiple inheritance, and abstract classes cannot implement multiple inheritance;

(2) Interfaces include methods, properties, events, indexers, cannot include fields, abstract classes can include fields, or they can include the implementation of methods;

(3) Interfaces can support callbacks, abstract classes do not support callbacks

(4) An interface can be a base class for value types and reference types, whereas an abstract class can only be used as a base class for reference types;

Phase II (Use phase): Just like question 2, this part of the people have a certain understanding of the foundation, but is a lack of certain practice, perhaps is to do a simple demo, then when to use abstract class, when to use the interface?

Analysing the second question, I propose 3 points:

The first suggestion, the basic concept is not just the concept of memory, to practice more, more thinking, and then more practice, then think, cycle several times, until cooked rotten in the heart;

The second suggestion, as far as possible in their own projects to use this knowledge, to use it, you can find problems, solve problems, will think;

The third proposal summarizes and sums up the knowledge points of the abstract classes and interfaces that you have used in the project.
When using abstract classes and interfaces, I summarize the experience of predecessors, give the following points, for reference only:

(1) When a component is designed to have multiple versions in the future, generally use abstract classes, such as designing DB DB in C #, you may be using SQL Server, MySQL, and later large projects may use ORACLE,DB, a large database system, So when we design the class, we design an abstract base class db, and let it have some common properties and methods of data, properties: Database connection name, version, database type, database common method: Open (), Close () method, etc.;

(2) When the design of the components to support common behavior action, you can consider the interface, for example, birds, humans, cars can have sound, this time you can design the interface, including the function of the called behavior, and then in each specific class implementation;

(3) in the derived class or interface that inherits the interface, once the interface needs to increase the behavior method is a more troublesome thing, must all the inheritance must implement its method, this time can be implemented in the derived class A new interface, to implement the unique action of the derived class, for example:

<summary>///implements a reptile's action interface///</summary>interface ianimalclimb{void Climb ();} &LT;SUMMARY&GT;///implements an action interface for a called animal///</summary>interface icry{void Cry ();} <summary>///implements an animal abstract class///</summary>public abstract class animal{//animal name public string name {get; set;} The color of the animal public string color {get; set;} Common methods for animal abstract classes public abstract void Sleep ();p ublic abstract void Breathe ();} <summary>///defines birds, the general method is to fly//</summary>public class bird:animal,icry{public override void Sleep () { Console.WriteLine ("Bird derived class inherits the sleep () method of the base class"); public override void Breathe () {Console.WriteLine ("Bird derived class inherits the Breathe () method of the base class"); Birds can inherit unified interface actions, for example: Call public void Cry () {Console.WriteLine ("Bird derived class inherits the method called by the interface Icry");}} <summary>///defines reptiles///</summary>public class Snake:animal, ianimalclimb{public override void Breathe ( {Console.WriteLine ("Snake derived class inherits the sleep () method of the base class");} public override void Sleep () {Console.WriteLine ("Snake derived class inherits the Sleep () method of the base class"); Reptiles can inherit a unified interface animal, for example: crawling PUBlic void Climb () {Console.WriteLine ("Snake derived class inherits the method of crawling of the Interface Ianimalclimb");}} 

The above code, just to explain the problem, relatively simple;
phase three (optimization phase): As in question 3, when we are doing an abstract class or interface, the first thing we consider is that we can use it, and the result is that the class or interface defined is much more difficult to maintain and extend. Or the intersection between classes, how to optimize the inheritance relationship? How can you make a program maintainable and extensible?
I personally recommend that you have the following aspects:
(1) to have a solid foundation of knowledge and Deep Foundation;
(2) to have a more asked, Dauth heart; ask more questions about abstract classes and interfaces, why not use an abstract class instead of an interface? Why is it appropriate to use the interface in this place?
(3) More to see how the predecessors of the design interface and class, this aspect of the information on-line search;
(4) Personal advice to look at the design patterns of this knowledge, because they are predecessors in the design of experience and ideas;

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.