5
Reply

How a non static method of an abstract class can be called?

Chetan Ranpariya

Chetan Ranpariya

14y
8.3k
0
Reply

    Hi,

    If you have sub class version method too, then need to elaborate the design by adding one interface which will hold all of your such methods, like below. This design can apply to any of your sub class by just implementing this interface.

    public class MyAbstractChild : MyAbstract,ICommon

    {

    public void CallMyAbstractMethod()

    {

    //Subclass version will be called

    ICommon obj = new MyAbstractChild();

    obj.MyAbstractMethod();

     

    //Main abstract class version will be called

    MyAbstract obj1 = new MyAbstractChild();

    obj1.MyAbstractMethod();

    }

    public new void MyAbstractMethod()

    {

    Console.WriteLine("I am abstract child");

    }

    }

    public abstract class MyAbstract : ICommon

    {

    public void MyAbstractMethod()

    {

    Console.WriteLine("I am abstract");

    }

    }

    public interface ICommon

    {

    void MyAbstractMethod();

    }

     

     

     

     

    Hi,

    As per your Q, non static method of an abstract class can call directly like below from sub class

    But you should not Hide or Overrride this method in your sub class

    public class MyAbstractChild : MyAbstract

    {

    public void CallMyAbstractMethod()

    {

    MyAbstractMethod();

    }

    }

    public abstract class MyAbstract

    {

    public void MyAbstractMethod()

    {

    Console.WriteLine("I am abstract");

    }

    }

    Hi Kiran,

    Thanks for taking interest and posting the reply.

    I am actually not getting the point you want to show from your reply. If you could elaborate further.

    Thanks,
    Chetan Ranpariya

    abstract class  can only inherited by  child class. and  everybods knows that abstract class can not be instantiated.  it makes rule but doesnot apply  byself gives to the child class. so it can be called by  desrived class object . object inherited  the metod of an astract class. by kiran adhlakha

    As everybody knows instance of an abstract class can not be initialized with its own object but it can be initialized with an instance of its derived class. In that case method version of derived class will be called always.
    To call method version of abstract class, there is a way.
    1. Create an abstract class (let say AClass) with a non virtual public method (let say method A()).
    2. Create a new class (let say ChildClass) and derive it form the abstract class.
    3. If needed create a new version of method A using "new" keyword.
    4. To call parent version of A
        AClass obj = new ChildClass();
        obj.A();    //This will call the method version from AClass

    There is one drawback of this approach that it looses the power of runtime polymorphism. I mean, what if I want to call child version of method A.
    In that case I have to cast the instance to the ChildClass type and then call method A, as following.
    ((ChildClass)obj).A() // This will call the method version from ChildClass.

    I hope this will help.

    Thanks,
    Chetan Ranpariya