2

Consider the following scenario:

public interface ITestInterface
{
   void TestMethod1();
   void TestMethod2();
}


public class TestParent
{
    void SomeMethod()
    {
        Console.Writeln("Method of test parent");
    }
}

public class Test1: TestParent, ITestInterface
{
  void TestMethod1()
  {
     Console.WriteLine("Implementation 1 of TestMethod1");
  }

  void TestMethod2()
  {
     Console.log("Same implementation");
  }
}

public class Test2: TestParent, ITestInterface
{
  void TestMethod1()
  {
     Console.WriteLine("Implementation 2 of TestMethod1");
  }

  void TestMethod2()
  {
     Console.log("Same implementation");
  }
}

TestParent is an existing class and Test1 and Test2 are the child classes of TestParent and implement ITestInterface.

In my above example, both the class have the same implementation for TestMethod2(). I was just wondering how to avoid duplicate code? I am planning to add couple more classes and they all have the same implementation for TestMethod2.

1
  • 1
    Add yet another class between the base class and child classes. Commented May 16, 2021 at 10:40

2 Answers 2

3

You need to add an intermediate class(TestParentExtension) which extends TestParent and implements TestMethod2(). You can then extend this intermediate class for Test1 and Test2 instead of TestParent.

Here you go. I cleaned up some syntax for you.

public interface ITestInterface {
  void TestMethod1();
  void TestMethod2();
}

public class TestParent {
  public void SomeMethod() {
    Console.WriteLine("Method of test parent");
  }
}

public class IntermediateParent: TestParent {
  public void TestMethod2() {
    Console.WriteLine("Same implementation");
  }
}

public class Test1: IntermediateParent, ITestInterface {
  public void TestMethod1() {
    Console.WriteLine("Implementation 1 of TestMethod1");
  }

}

public class Test2: IntermediateParent, ITestInterface {
  public void TestMethod1() {
    Console.WriteLine("Implementation 2 of TestMethod1");
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I would have accepted this as an answer. But, when Tim answered I missed some context but he is kinda implying the same.
2

Why don't you use an (abstract) base class?

public abstract class TestBase: TestParent, ITestInterface
{
    void SomeMethod()
    {
        Console.Writeln("Method of test parent");
    }

    #region ITestInterface

    public void TestMethod1()
    {
        Console.WriteLine("Implementation 1 of TestMethod1");
    }

    public void TestMethod2()
    {
        Console.log("Same implementation");
    }

    #endregion
}

public class Test1 : TestBase
{
}

public class Test2 : TestBase
{
}

3 Comments

I have updated my question adding some more context. TestParent is an existing class and I need to extend that class which implements the ITestInterface.
@kaikadi-bella: since you have provided pseudo classes it's hard to suggest how you could refactor your code. For instance, it's not possible to let TestParent implement the interface? Or another way, let TestBase inherit from TestParent(edited my answer).
TestParent cannot implement the interface because there are other derived classes of TestParent which implement some other interface. Adding TestBase inherit from TestParent will do. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.