Search code examples
c#overloadingabstract-class

Overloaded functions with inherited parameter


I'm trying to do something like this but I'm getting an error along the lines of:

Cannot convert from AbstractExampleClass to SpecificExampleClassA.

So I'm probably going in the wrong direction with this code, can anyone tell me what's wrong and how to fix it.

public abstract class AbstractExampleClass 
{
    // Code goes here
}

public class SpecificExampleClassA : AbstractExampleClass 
{

}

public class SpecificExampleClass : AbstractExampleClass 
{

}

public class handler 
{
    public void Handle(AbstractExampleClass aec)
    {
        HandleSpecific(aec);
    }

    public void HandleSpecific(SpecificExampleClassA a)
    {
        // DoSomething
    }

    public void HandleSpecific(SpecificExampleClassB b)
    {
        // DoSomething Else
    }

}

Solution

  • Overloading is resolved at compile time, so you can't expect it to decide which overload to call depending on the runtime type of aec. The overload to call must be decided at compile time.

    What you really need here is the "subtyping" kind of polymorphism (aka just "polymorphism" in C# terminology), not ad hoc polymorphism (aka "overloading").

    Move the handler methods in the subclasses instead:

    public abstract class AbstractExampleClass 
    {
        public abstract void Specific();
    }
    
    public class SpecificExampleClassA : AbstractExampleClass 
    {
    
        public override void Specific()
        {
            // DoSomething
        }
    }
    
    public class SpecificExampleClass : AbstractExampleClass 
    {
    
        public override void Specific()
        {
            // DoSomething Else
        }
    }
    
    public class handler 
    {
        public void Handle(AbstractExampleClass aec)
        {
            aec.Specific();
        }
    }