How to implement hidden abstract method?

241 views Asked by At

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;
  }
}
1

There are 1 answers

3
Jamiec On

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;
  }
}