C# - Interface Reimplementation

Shervan360 1,641 Reputation points
2024-12-06T14:49:13.8533333+00:00

Hello,

I've read an article about Interface implementation, I don't understand the second reason.

Could you please explain and write an example? I really appreciate any help you can provide.

Alternatives to interface reimplementation

Even with explicit member implementation, interface reimplementation is problematic for a couple of reasons:

  1. The subclass has no way to call the base class method.
  2. The base class author might not anticipate that a method will be reimplemented and might not allow for the potential consequences
.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,040 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,194 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 69,276 Reputation points
    2024-12-06T17:14:52.54+00:00

    the second reason also applies to virtual methods. It is the main argument against class inheritance and why it is often considered an anti pattern.

    when a class method is invoked it can change internal state, and the class definition may count on this. this can be mitigated by the virtual method call the base method, but this may introduce other side effects. as the class hierarchy gets deeper, this becomes more of an issue as there may be many virtual method implementations.

    google "case against class inheritance" or "class inheritance anti pattern" for more information

    0 comments No comments

  2. Jiale Xue - MSFT 48,686 Reputation points Microsoft Vendor
    2024-12-09T14:49:37.4533333+00:00

    Hi @Shervan360 , Welcome to Microsoft Q&A,

    When a base class implements an interface, it may provide a specific implementation for one or more members of the interface. If a derived class reimplements the same interface, it effectively overrides the behavior of the base class's implementation of the interface. For example, a base class may rely internally on its own implementation of interface members. If a derived class reimplements an interface, it may change the behavior of the interface methods in a way that breaks assumptions made by the base class.

    using System;
    
    namespace xxx
    {
        interface IExample
        {
            void DoWork();
        }
    
        class BaseClass : IExample
        {
            public void DoWork()
            {
                Console.WriteLine("BaseClass: Doing work...");
            }
    
            public void CallWork()
            {
                DoWork();
            }
        }
    
        class DerivedClass : BaseClass, IExample
        {
            // Reimplementing the interface
            void IExample.DoWork()
            {
                Console.WriteLine("DerivedClass: Doing different work...");
            }
        }
    
        class Program
        {
            static void Main()
            {
                IExample example1 = new BaseClass();
                example1.DoWork(); // Output: BaseClass: Doing work...
    
                IExample example2 = new DerivedClass();
                example2.DoWork(); // Output: DerivedClass: Doing different work...
    
                DerivedClass derived = new DerivedClass();
                derived.CallWork(); // Output: BaseClass: Doing work...
    
            }
        }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.