0

I have two abstract classes AbstractClass1 and AbstractClass2 that both should have the same method names Meth1.

Now what if my AbstractClass1 inherits from AbstractClass2. Currently it is throwing me a compile time error as "Hides Inherited Abstract Member". Below is my code.

 abstract class AbstractClass1 : AbstractClass2
 {
    public abstract string Meth1();
 }

 abstract class AbstractClass2
 {
    public abstract string Meth1();
 }
Random Dev
  • 51,810
  • 9
  • 92
  • 119
Rahul
  • 156
  • 2
  • 21
  • 2
    as both methods are abstract there is **no** reason to give a `new` declaration in your `AbstractClass1` at all - what are you really trying to do? – Random Dev Jul 06 '15 at 06:52
  • If `AbstractClass1` extends `AbstractClass2`, why do you need to declare the method again? – Glorfindel Jul 06 '15 at 06:52
  • you cab override the Meth1() in AbstractClass1.But why you need such kind of a declaration? – Joseph Jul 06 '15 at 06:54
  • why do you need this ? – farid bekran Jul 06 '15 at 07:02
  • Hi, Its not a real time scenario Carsten, Glorfindel, Joseph and Farid. I was just doing it for my R and D as it was asked me in my recent interviews. Thanks a lot for your replies :) – Rahul Jul 06 '15 at 07:33

2 Answers2

3

You can use abstract override on derived class method to avoid compile time error.

abstract class BaseAbstract
{
    public abstract string Meth1();
}

abstract class ChildAbstract : BaseAbstract
{
    public abstract override string Meth1();
}

BUT please note that this is not quite right place to use abstract override. Usually abstract override is used when deriving an abstract class from non abstract class. to ensure that derived class implements the method. (see this)

Take a look at below code

class BaseNonAbstract
{
      public virtual string Meth1()
      {
         // do something
         return string.Empty;
      }
}
abstract class ChildAbstract : BaseNonAbstract
{
    public abstract override string Meth1();
}
Community
  • 1
  • 1
farid bekran
  • 2,684
  • 2
  • 16
  • 29
2

Try this

abstract class AbstractClass1 : AbstractClass2
{
}


abstract class AbstractClass2
{
    public abstract string Meth1();
}