Recognizing the differences between abstract classes and interfaces

Source: Internet
Author: User

 

Because there are very few projects used in normal times, I forgot to read this basic problem. The same information is found on the Internet. Today, I will test these statements myself.

Iii. Differences between abstract classes and interfaces:
1. class is the abstraction of objects. abstract classes can be understood as classes as objects. abstract classes are called abstract classes. the interface is just a behavior specification or provision. Microsoft's custom interface always carries the able field behind it to prove that it represents a class "I can do it... ". 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.
2. The interface basically does not have any specific characteristics of inheritance. It only promises the methods that can be called;
3. A class can implement several interfaces at a time, but only one parent class can be extended.
4. interfaces can be used to support callback, but inheritance does not.
5. the abstract class cannot be sealed.
6. 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.
7. Similar to a non-abstract class, an abstract class must provide its own implementation for all the members of the interface listed in the base class list of this class. However, the abstract class is allowed to map interface methods to abstract methods.
8. abstract classes implement a principle in oop that separates mutable from immutable. Abstract classes and interfaces are defined as immutable classes, while variable class classes are implemented.
9. A good interface definition should be specific and functional, rather than multi-functional, otherwise it will cause interface pollution. If a class only implements a function of this interface and has to implement other methods in the interface, it is called interface pollution.
10. Avoid using inheritance to implement the build function, but use black box multiplexing, that is, object combination. As the hierarchy of inheritance increases, the most direct consequence is that when you call a class in this group, you must load all of them into the stack! The consequences can be imagined (based on the stack principle ). At the same time, some interested friends can note that Microsoft often uses the object combination method when building a class. For example, in asp.net, the Page class has Server Request and other attributes, but in fact they are all objects of a certain class. This object of the Page class is used to call the methods and attributes of other classes. This is a very basic design principle.
11. If an abstract class implements an interface, you can map the methods in the interface to the abstract class as an abstract method without having to implement it. Instead, you can implement the methods in the subclass of the abstract class.

 

Article 1: Simply put, an abstract class is the abstraction of A Class. An interface is a behavior specification, and others are too XX to understand.

Article 2: it can be understood that the inherited interface is only used to use the methods in the interface

Article 3: A class can inherit from multiple interfaces and only one class. An interface can also inherit from multiple interfaces. The difference is whether an interface can inherit from a class, write an interface to inherit a class.

Interface Interface1: Class1
{
}

 

Program error:

If you write an abstract class so that it inherits interfaces and classes, no error is reported.

Article 4: The interface supports callback, but the abstract class does not. I found an example on the Internet to try it.

Code

Public class CallBackDemo
{
Public static void CallBackFunction ()
{
// Pass the object to the interface type variable (IBack)
Controller obj = new Controller (new CallBack ());
// Call the object Method
Obj. Start ();
}
}

Public interface IBack
{
Void Run ();
}
Public class CallBack: IBack
{

# Region IBack Members

Public void Run ()
{
Console. WriteLine (DateTime. Now );
}

# Endregion
}


Public class Controller
{
Public IBack CallBackObj = null;
Public Controller (IBack obj)
{
CallBackObj = obj;
}

Public void Start ()
{
Console. WriteLine ("press any key on the keyboard to display the current time until you Press Esc to exit ");
While (Console. ReadKey (true). Key! = ConsoleKey. Escape)
{
CallBackObj. Run ();
}
}
}

 

This example is very simple, that is, as long as the ESC key is not pressed, it will always execute the current output time. The parameters of the Controller class constructor are of the interface type, when you pass in an object of its implementation class, the method of this object will be called to output the current time.

The result is as follows:

Then, use the abstract class to replace this interface to see if this function can be implemented:

Code

1 public class CallBackDemo
2 {
3 public static void CallBackFunction ()
4 {
5 // pass the object to the interface type variable (IBack)
6 Controller obj = new Controller (new CallBack ());
7 // call the object Method
8 obj. Start ();
9}
10}
11 // public interface IBack
12 //{
13 // void Run ();
14 //}
15 // public class CallBack: IBack
16 //{
17
18 // # region IBack Members
19
20 // public void Run ()
21 //{
22 // Console. WriteLine (DateTime. Now );
23 //}
24
25 // # endregion
26 //}
27 public abstract class IBack
28 {
29 public abstract void Run ();
30}
31 public class CallBack: IBack
32 {
33 public override void Run ()
34 {
35 Console. WriteLine (DateTime. Now );
36}
37}
38 public class Controller
39 {
40 public IBack CallBackObj = null;
41 public Controller (IBack obj)
42 {
43 CallBackObj = obj;
44}
45
46 public void Start ()
47 {
48 Console. WriteLine ("press any key on the keyboard to display the current time until you Press Esc to exit ");
49 while (Console. ReadKey (true). Key! = ConsoleKey. Escape)
50 {
51 CallBackObj. Run ();
52}
53}
54}

 

Now let the Controller constructor accept an IBack type variable, the same can be implemented. This difference is a little far-fetched.

Article 5: The abstract class cannot be sealed, meaning that the abstract class cannot be sealed and the interface can be sealed. After being sealed, it cannot be inherited. Therefore, an error is reported if the following code is used to inherit the code. This error should also occur, and the interface cannot be sealed. This article cannot be said.

Write a statement:

Public sealed abstract class IBack {} Error

Obviously, the abstract class IBack cannot be sealed or static.

When sealed is used on the interface public sealed interface IBack {}, the following error occurs:

It means that the keyword sealed cannot be used on the interface. Here the word "item" is used, that is, the interface is equivalent to a variable.

Both of them will report an error. This difference is also a little far-fetched.

Article 6: 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.

Methods in abstract classes are virtual, but virtual keywords are not provided for modification. If an error is reported, the methods in interfaces are public and cannot be declared as virtual. This is obvious. The Code is as follows:

1 public interface IBack
2 {
3 virtual void Run ();
4}

 

The following error is reported:

An error is also reported if the methods in the abstract class are expressed as virtual.

Public abstract class IBack
{
Public virtual abstract void Run ();
} The same error will be reported: the abstract keyword does not need to be declared as virtual. It seems that abstract and virtual are somewhat similar, and abstract functions are also virtual. Article 7: The first sentence is not the difference between the two. It is the same thing in common. The second sentence and the second sentence mean that an interface can implement an abstract class, but not a method in an abstract class, however, this method is weird, and it is estimated that this is not used in the project.

 

Article 8: abstract classes implement a principle in oop that separates mutable from immutable. Abstract classes and interfaces are defined as immutable classes, while variable class classes are implemented. This article also describes the similarities, not the differences.

Article 9: A good interface definition should be specific and functional, rather than multi-functional, otherwise it may cause interface pollution. If a class only implements a function of this interface and has to implement other methods in the interface, it is called interface pollution. Let alone the difference.

Article 10: Avoid using inheritance to implement the build function, but use black box multiplexing, that is, object combination. As the hierarchy of inheritance increases, the most direct consequence is that when you call a class in this group, you must load all of them into the stack! The consequences can be imagined (based on the stack principle ). At the same time, some interested friends can note that Microsoft often uses the object combination method when building a class. For example, in asp.net, the Page class has Server Request and other attributes, but in fact they are all objects of a certain class. This object of the Page class is used to call the methods and attributes of other classes. This is a very basic design principle. The difference is that composite objects are used to avoid multiple inheritance.

Article 12: similar to the last half of Article 7, an abstract class can implement interfaces, but not specific implementations, but in its subclass. This is no difference.

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.