Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract class design

Is this an acceptable design ??

Abstract class

public abstract class SomethingBase
{ 
   public abstract int Method1(int number1);
   public abstract int Method2(int number2); 
} 

public class Class1 : SomethingBase
{
   public override int Method1(int number1)
   {
      //implementation
   }

   //i dont want this method in this class
   public override int Method2(int number2)
   {
        throw new NotImplementedException();
   }
}

public class Class2 : SomethingBase
{

   //i dont want this method in this class
   public override int Method1(int number1)
   {
     throw new NotImplementedException();
   }

   public override int Method2(int number2)
   {
    //implementation
    }
}

I mean the situation if I need method1 in my Class1 and the method2 not and vica verse for Class2. In fact the methods are excluding each other in the derived classes.

like image 302
user137348 Avatar asked Nov 28 '22 12:11

user137348


1 Answers

It is not an acceptable design as it stands. To make it much more acceptable, you could implement two interfaces on the abstract class. One for Method1() and one for Method2(), and simply use the interface object to pass that around.

You really don't want to use the Class1 and Class2 definitions, as there is a danger of your clients calling the method that is not implemented. So I'd suggest to use interfaces and use these instead.

like image 191
Wim Avatar answered Dec 04 '22 10:12

Wim