Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to prevent inheritance of an abstract class

Consider the code:

public abstract class Base
{
   public Dictionary<int, string> Entities { get; private set; }

   protected Base()
   {
      Entities = new Dictionary<int, string>();
   }
}

public abstract class A : Base
{
   public abstract void Update();
}

public abstract class B : Base
{
   public abstract void Draw();
}

Is it possible to restrict classes (in the same assembly) from inheriting from Base, forcing them to inherit from either A or B?

like image 503
Steve Avatar asked Oct 19 '25 10:10

Steve


1 Answers

To elaborate a little on my comment. This isn't supported, so the answer is no. The only contrived way of trying this that comes to mind (that obviously wouldn't work) would be to have a sealed abstract class, which is nonsensical, as neither A nor B, nor anything else, could then inherit either.

There is no discriminator that allows us to say, "sealed for", for instance.

like image 102
Grant Thomas Avatar answered Oct 21 '25 23:10

Grant Thomas