Types of Inheritance C# Help

Let’s start off by reviewing exactly what C# does and does not  support as far as inheritance is concerned.

Implementatlon versus Interlace Inheritance

In object-oriented programming, there are two distinct types of inheritance – implementation inheritance and interface inheritance:

  1.  implementation is to be oven. This type of inheritance is . most useful when you need to add functionality to an exis . g type, or when a number of .related types share a significant amount of common functionality. A good example of this comes in the Wmdows Forms classes, Specific examples are the base class System. Windows.Forms. Control, which provides avery sophisticated implementation of a generic WUldowscontrol, and numerous other classes such as System. Windows.Forms .TextBox and System. Windows.Forms .ListBox that are derived from Control and that override functions or provide new functions to implement specific types of control.
  2.  Interface inheritance means that a type inheritance that a ~ Certain avAiJab For example, certain types can indicate that they provide a resource cleanup method called Dispose () by deriving from an interface, System .Disposable. Because the way that one type cleans up resources is likely to be very different from the way that another type cleans up resources, there is no point in defining any common implementation, so interface inheritance is appropriate here. Interface inheritance is often regarded as providing a contract: By deriving from an interface, a type is guaranteed to provide certain functionality to clients.

Traditionally, languages such as C++ have been very strong on implementation inheritance. Indeed, implementation inheritance has been at the core of the C++ programming model. Although Visual Basic 6 did not support any implementation inheritance of classes, it did support interface inheritance thanks to its underlying COM foundations.

C# supports both implementation and interface inheritance. Both are baked into the framework and the language from the ground up, thereby allowing you to decide which to use based on the architecture of the application.

Multiple Inheritance

Some languages such as C++ support what is known as .,ultt,lt irrIttriItnt«, in .a c:1 cleriv from more than one other e benefits of using multiple inheritance are debatable: On one end, there is no doubt that it is possible to use multiple inheritance to write extremely sophisticated, yet compact, code, as demonstrated by the C++ ATL library. On the other hand, code that uses multiple implementation inheritance is often difficult to understand and debug (a point that is equally well demonstrated by the C++ ATL library). As mentioned, making it easy to write robust code was one of the crucial design goals behind the development of C#. Accordingly, C# does not support multiple implementation inheritance. It does, however, allow types to be derived from multiple interfaces – multiple interface inheritance. This means that a CII class can be derived from one other class, and any , number of interfaces. Indeed, we can be more precise: Thanks to the presence of System. Object as a common base type, every C# class (except for Object) has exactly one base class, and may additionally “have any number of base interfaces.

Structs and Classes

This distinguishes between structs (value types) and classes (reference types). One restriction of using a struct is that structs do not support inheritance, beyond the fact that every struct is automatically derived from System. Value Type. In fact, we should be more careful. It’s true that it is not possible to code a type hierarchy of structs; however, it is possible for structs to implement interfaces. In other words, structs don’t really support implementation inheritance, but they do support interface inheritance.

Posted on October 28, 2015 in Inheritance

Share the Story

Back to Top