0

I have ChildClassA, ChildClassB and ChildClassC.

They all inherit from ParentClass.

How can I declare an already implemented method for A and B, but which is not needed in C?

  1. Implementation in ParentClass doesnt make sense, since I only need it in A and B.

  2. C# does not support multiple inheritance (if this would even be my best option).

  3. An interface is not helping me because I cannot implement a method there.

MichaelS
  • 171
  • 11
  • 3d point (about interfaces) is actually not true since [C# 8](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/default-interface-methods). Though personally I prefer not to use them due to some issues/not very clear behaviour ([1](https://stackoverflow.com/q/62334232/2501279) [2](https://stackoverflow.com/questions/64295242/unexpected-behavior-of-a-c-sharp-8-0-default-interface-member)). – Guru Stron Aug 19 '21 at 12:38
  • What do you mean `How can I add an already implemented method for A and B, but not for C?` Already implemented where? In `ParentClass`? – Patrick Tucci Aug 19 '21 at 12:39
  • `How can I add an already implemented method for A and B, but which is not needed in C?` Add a ICommonToAB interface and have A and B implement it. Both of their implementations will be one line long - calling your existing logic (in class D). – mjwills Aug 19 '21 at 12:42
  • I did not phrase that very well: What I mean is that the method is already defined. It does not need any change to be used on both A and B. But it would not work on C and I dont need it on C – MichaelS Aug 19 '21 at 12:42
  • 2
    Composition: https://dotnetfiddle.net/0OGIH5 – Fildor Aug 19 '21 at 12:44
  • But the logic in class `D`. A and B both have an instance of `D` internally where they call the shared logic. – mjwills Aug 19 '21 at 12:45
  • Does this answer your question? [How to hide (remove) a base class's methods in C#?](https://stackoverflow.com/questions/1125746/) and [Can publicly inherited methods be hidden](https://stackoverflow.com/questions/106383/) and [How to hide all overrides off parent method](https://stackoverflow.com/questions/14062165/) and [how to hide parent property from child class](https://stackoverflow.com/questions/2162369/) and [Prefer composition over inheritance?](https://stackoverflow.com/questions/49002/) and [Method Hiding in C#](https://dotnettutorials.net/lesson/function-hiding-csharp/) –  Aug 19 '21 at 12:45
  • My problem: I have a method which does something but is only needed in Class A and B and not C. So I dont want to inherit that method with the ParentClass. But I do not want to add it to A and B and have to change the method in A and B if I need to, since if the method changes, it will change the same way for A and B – MichaelS Aug 19 '21 at 12:46
  • 2
    Did you have a look into the fiddle? It's exactly what you describe in your last comment and what mjwills is trying to explain to you. – Fildor Aug 19 '21 at 12:48
  • 1
    yeah its hard to write and read at the same time :-) the fiddle is exactly what i am trying to accomplish. thank you guys for your time – MichaelS Aug 19 '21 at 12:49

2 Answers2

3

What you are looking for is a form of Composition:

using System;
                    
public class Program
{
    public static void Main()
    {
        var a = new ChildA();
        a.SomeMethod();
        
        var b = new ChildB();
        b.SomeMethod();
        
        var c = new ChildC();
        // c.SomeMethod(); Doesn't have SomeMethod
    }
}

public class ParentClass
{
}

public class ChildA : ParentClass, IHavingSomeMethod
{
   private readonly IHavingSomeMethod _sms;
   
   public ChildA()
   {
       _sms = new HavingSomeMethodService(); // Have it injected instead!
   }
   
   public void SomeMethod() => _sms.SomeMethod();
}

public class ChildB : ParentClass, IHavingSomeMethod
{
   private readonly IHavingSomeMethod _sms;
   
   public ChildB()
   {
       _sms = new HavingSomeMethodService(); // Have it injected instead!
   }
   
   public void SomeMethod() => _sms.SomeMethod();
}

public class ChildC : ParentClass
{
}

public interface IHavingSomeMethod
{
    void SomeMethod();
}

public class HavingSomeMethodService: IHavingSomeMethod
{
   public void SomeMethod()
   {
      Console.WriteLine("SomeMethod");
   }
}

Try it out

Fildor
  • 14,510
  • 4
  • 35
  • 67
-1

ParentClass inherits to

  • CommonClass

  • and ChildClassC

CommonClass inherits to

  • ChildClassA
  • and ChildClassB

CommonClass defines the method