[WYSIWYG] An abstract class and an interface is confusing.

Source: Internet
Author: User

 

 

I. Problem introduction:

When looking at the source code of some open-source projects, we often see the following structure design:

 

I am very puzzled.

Ii. Problem Analysis:

To analyze the problem, I wrote several test classes and interfaces, as shown below:

Interface:

public interface A{        string Test1();        void Test2();}

Abstract class:

Public abstract class B: A {# region A member public virtual string Test1 () {Console. writeLine ("abstract B Test1"); return "B Test1";} public virtual void Test2 () {Console. writeLine ("abstract B Test2");} # endregion}

 

Classes that inherit abstract classes and interfaces at the same time:

public   class C :B,A{    }

 

Only inherit specific classes of abstract classes:

 public  class D :B{    }

 

 

The test also has the following usage:

             A a1 = new C();                A a2 = new D();                B b1 = new C();                B b2 = new D();                a1.Test1();                a2.Test1();                Console.WriteLine(a1.GetType().GetInterfaces()[0]);                Console.WriteLine(a2.GetType().GetInterfaces()[0]);                Console.WriteLine(a1.GetType().BaseType);                Console.WriteLine(a2.GetType().BaseType);                Console.WriteLine("-----------------------------------------------");                Console.WriteLine(b1.GetType().GetInterfaces()[0]);                Console.WriteLine(b2.GetType().GetInterfaces()[0]);                Console.WriteLine(b1.GetType().BaseType);                Console.WriteLine(b2.GetType().BaseType);

 

The result is as follows:

That is to say, the two classes are essentially different from the running results.

 

I carefully checked their IL code:

.class public auto ansi beforefieldinit D    extends FMS_Refacting.interfaceAndabsctract.B{    .method public hidebysig specialname rtspecialname instance void .ctor() cil managed    {        .maxstack 8        L_0000: ldarg.0         L_0001: call instance void FMS_Refacting.interfaceAndabsctract.B::.ctor()        L_0006: ret     }} 

 

 

.class public auto ansi beforefieldinit C    extends FMS_Refacting.interfaceAndabsctract.B    

Implements FMS_Refacting.interfaceAndabsctract.A

{    .method public hidebysig specialname rtspecialname instance void .ctor() cil managed    {        .maxstack 8        L_0000: ldarg.0         L_0001: call instance void FMS_Refacting.interfaceAndabsctract.B::.ctor()        L_0006: ret     }} 

The only difference is that the red part is marked. But this will not affect anything, because according to the definition of inheritance, D implements A through B, which has no difference with the above. So there is no difference here.

That is to say, the two forms are the same !!!

 

We know:

Interface:Is an abstract type that contains a set of virtual methods. Each method has its name, parameter, and return value. Interface methods cannot contain any implementations. CLR allows interfaces to include events, attributes, indexers, static methods, static fields, static constructors, and constants. Note: C # cannot contain any static members. A class can implement multiple interfaces. When a class inherits an interface, it must not only implement all methods defined by this interface, but also implement all methods inherited from other interfaces.

Abstract class:Provides multiple Derived classes to share the public definitions of the base class. It can provide both abstract methods and non-abstract methods. Abstract classes cannot be instantiated. They must be inherited by the derived classes to implement their abstract methods. Therefore, abstract classes cannot use the new keyword or be sealed. If the derived class does not implement all abstract methods, the derived class must also be declared as an abstract class. In addition, the implementation abstract method is implemented by the override method.

 

An advantage of comparing abstract classes and interfaces is that some functions can implement specific methods rather than declaring abstract methods, the interface can only declare Abstract METHODS (and enforce subclass Implementation). Therefore, using an abstract class to implement an interface can implement some common methods, abstract methods can also be called in these specific implementation methods, so repeated code in the subclass is reduced.

 

Therefore, I think the inheritance form of D is quite common, but why does the inheritance form of C appear again? My conjecture:

  • 1. Should the framework be easy to expand?
  • 2. Is it related to the design model?
  • 3. Beautiful?
Iii. Final

I am still not sure why this design is required. Is there any special reason why I didn't think of it? If you know, please let me know and I will keep searching for the reason. I am very grateful.

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.