0

If the abstract method was hidden by previous abstract inheritance, how can I implement it in concrete class? I have no way to access it, only the access to the inheriting abstract method with same name. The system reports error even it's masked. Here is an example:

abstract class MyAbClass1
{
  abstract public int MyMethod(int x);
}
abstract class MyAbClass2 : MyAbClass1
{
  abstract public int MyMethod(int x);
}
class Myderived : MyAbClass2
{
  override public int MyMethod(int x)
  {
    return x;
  }
}
oversea
  • 1
  • 1
  • 7
    Including actual class definitions to illustrate the problem would help. Let the people at home play along. – Jeroen Mostert Mar 04 '21 at 08:58
  • It's not clear what you mean by "previous abstract inheritance" or "masked". Those aren't commonly-used terms. As Jeroen says, code which demonstrates your issue will go a long way towards getting an answer – canton7 Mar 04 '21 at 09:08
  • 2
    Code shown in the post does not compile ([CS0533](https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0533) ) since it is not legal in C# to hide abstract method... Please [edit] the question to actually demonstrate what you mean "abstract method was hidden by previous abstract inheritance". – Alexei Levenkov Mar 04 '21 at 10:13
  • 1
    Probably should be closed as duplicate of https://stackoverflow.com/questions/39375263/cannot-hide-inherited-member-compiler-error which clarifies that such code is impossible to write... – Alexei Levenkov Mar 04 '21 at 10:17

1 Answers1

1

If MyAbClass2 does not have an implementation of MyMethod it should not even specify it - it remain abstract. If it does, then it should use the override keyword, whereby Myderived can also override.

abstract class MyAbClass1
{
  abstract public int MyMethod(int x);
}
abstract class MyAbClass2 : MyAbClass1
{
  // no implementation of MyMethod
}
class Myderived : MyAbClass2
{
  override public int MyMethod(int x) // this now works
  {
    return x;
  }
}

or

abstract class MyAbClass1
{
  abstract public int MyMethod(int x);
}
class MyAbClass2 : MyAbClass1 // no longer abstract
{
  override public int MyMethod(int x) 
  {
    return x*2;
  }
}
class Myderived : MyAbClass2
{
  override public int MyMethod(int x) // this now works
  {
    return x; // or perhaps base.MyMethod(x) - 42;
  }
}
Jamiec
  • 133,658
  • 13
  • 134
  • 193