-2

I am just trying to understand if the below is possible.

I have customer, specialCustomer and goldCustomer classes. Special inherits from Customer and GoldCustomer inherits from SpecialCustomer.

How can i access Customer class's implementation of Add() from GoldCustomer? Using base.Add() calls only the immediate parent's Add()

public class Customer
{
    public virtual void add()
    {
        Console.WriteLine("Base Add");

    }
}

public class SpecialCustomer: Customer
{
    public override void add()
    {            
        Console.WriteLine("SPL CUST Add");
    }
}

public class GoldCustomer : SpecialCustomer
{

    public new void add()
    {
        base.add();// cals the immediate parent.
        //How to call the method on Customer base class(Parent's parent)
        Console.WriteLine(" chid GOLD CUST Add");
    }

}
jothi
  • 332
  • 1
  • 5
  • 16

2 Answers2

0

My first suggestion is to avoid such situation if you can (software is complex, why make it even more complicated?).

The second suggestion, if you cannot avoid the situation, is cast this to desired base type and call the desired method, like ((Customer)this).add()

But can you explain why you need to do this kind of thing? It may be that your design needs some refactoring.

felix-b
  • 8,178
  • 1
  • 26
  • 36
0
    public class Customer
    {
        public virtual void add()
        {
            C1();
        }


        public void C1()
        {
            Console.WriteLine("Base Add");
        }
    }

Your code should look like this and after that just call new GoldCustomer.C1();

mybirthname
  • 17,949
  • 3
  • 31
  • 55