What is implementation inheritance and interface inheritance?

Implementation inheritance is when a class inherits all members of the class from which it is derived. Interface inheritance is when the class inherits only signatures of the functions from another class.

In the context of .NET, implementation inheritance and interface inheritance refer to two fundamental concepts in object-oriented programming:

  1. Implementation Inheritance:
    • Implementation inheritance, also known as class inheritance, is the mechanism by which a class can inherit properties, methods, and behavior from another class.
    • In implementation inheritance, a subclass (or derived class) inherits the attributes and behaviors of a superclass (or base class).
    • In C#, this is achieved using the : symbol after the subclass name, followed by the name of the superclass. For example:
      csharp
      class BaseClass {
      // Base class members
      }

      class DerivedClass : BaseClass {
      // Derived class members
      }

    • The derived class can extend the functionality of the base class by adding new members or overriding existing ones.
  2. Interface Inheritance:
    • Interface inheritance allows a class to inherit only method signatures (declarations) from one or more interfaces.
    • An interface defines a contract that implementing classes must adhere to by providing concrete implementations for the methods declared in the interface.
    • In C#, interface inheritance is achieved using the interface keyword. A class can implement multiple interfaces.
    • For example:
      csharp
      interface IExampleInterface {
      void Method1();
      void Method2();
      }

      class ExampleClass : IExampleInterface {
      public void Method1() {
      // Implementation of Method1
      }

      public void Method2() {
      // Implementation of Method2
      }
      }

    • Interface inheritance allows for achieving polymorphism and enables objects of different classes to be treated interchangeably if they implement the same interface.

In summary, implementation inheritance involves inheriting properties and behavior from a superclass, while interface inheritance involves inheriting method signatures from interfaces. Both mechanisms play crucial roles in achieving code reusability, maintainability, and flexibility in object-oriented programming.