Peter Lange
Peter Lange

Reputation: 2886

Object Method Inheritance in C#

Ok, this takes a bit of setup first.

public interface IPolicyObjectAdapter
{
    PolicyImage GetPolicyImage(BridgePolicy policy);
}

public class BridgePolicyAdapter : IPolicyObjectAdapter
{
    protected virtual PolicyImage GetPolicyImage(BridgePolicy policy)
    {
         AddPolicyInformation(policy);
    }

    protected virtual void AddPolicyInformation(BridgePolicy policy)
    {
       //does stuff
    }
}


public class HSPolicyAdapter : BridgePolicyAdapter, IPolicyObjectAdapter
{
     protected override void AddPolicyInformation(BridgePolicy policy)
     {
          base.AddPolicyInformation(policy);
          //does more stuff
     }
}

When I execute the following code, I expect the code to enter into the HSPolicyAdapter's AddPolicyInformation method. But it never does. It just goes straight into the BridgePolicyAdapters AddPolicyInformation method.

IPolicyObjectAdapter Adapter = null;
Adapter = new HSPolicyAdapter();
PolicyImage image = Adapter.GetPolicyImage(policy);

I am sure I am missing something so obvious it will hurt, but my brain isnt working right now. What am I missing?

Upvotes: 1

Views: 96

Answers (1)

David Osborne
David Osborne

Reputation: 6821

I cobbled together this interpretation and it worked fine for me:

public class BridgePolicy
{
    public string Name { get; set; }
}

public class PolicyImage
{
    public string Name { get; set; }
}

public interface IPolicyObjectAdapter
{
    PolicyImage GetPolicyImage(BridgePolicy policy);
}

public class BridgePolicyAdapter : IPolicyObjectAdapter
{

    protected virtual void AddPolicyInformation(BridgePolicy policy)
    {
        //does stuff
    }


    public virtual PolicyImage GetPolicyImage(BridgePolicy policy)
    {
        AddPolicyInformation(policy);

        return null;
    }
}


public class HSPolicyAdapter : BridgePolicyAdapter, IPolicyObjectAdapter
{
    protected override void AddPolicyInformation(BridgePolicy policy)
    {
        base.AddPolicyInformation(policy);
        //does more stuff
    }
}


class Program
{
    static void Main(string[] args)
    {
        BridgePolicy p = new BridgePolicy();
        IPolicyObjectAdapter Adapter = null;
        Adapter = new HSPolicyAdapter();
        PolicyImage image = Adapter.GetPolicyImage(p);
    }
}

Upvotes: 1

Related Questions