Need a parent class that holds a method that use (or not) a parameter

Good afternoon to everybody,

In order to make a pluggable state machine using ScriptableObject, I need to make a parent class for the action.

   public abstract class ParentClass
    public abstract void DoSomething()

But, sometimes, I want the child class be able to use a parameter in the DoSomething method. For example

public class ChildClass

Is there an elegant way to declare the method in the parent class in order to be able to pass or not an argument in parameter ?

I hope to be clear,


No, there is not. A method with a parameter or without are two completely different method since the signatures are different. What you can do is declare both method overloads. Keep in mind that an abstract method has to be overridden while a virtual method does not necessarily.

However you should rethink your overall concept. When you use polymorphism you actually don’t want to know which concrete instance you are using. That’s the point of abstraction. So you have one “interface” which you use. Your question uses unspecific abstract names like ParentClass and ChildClass so we have no idea what your actual usage is. If most the time you need the parameter you just declare your method with the parameter. If you don’t need the parameter you just pass “null” or in your case. However as I said since you don’t know which class might be called, each class has to be able to handle such a non-specified values. To me your requirement sounds like you don’t really have any concept for your abstract interface. Without more details we can not really suggest what you should do.