Search code examples
c#oopabstract-classabstract-methods

Why the abstract method is not executing in the following code?


I am pasting the below code :

 abstract public class test
    {
        abstract public void add(int a, int b);

    }

    class Program : test
    {
        public void add(decimal a, decimal b)
        {
            decimal c = a + b;
            Console.WriteLine("Decimal Addition : {0}", c);
        }

        public override void add(int a, int b)
        {
            int c = a + b;
            Console.WriteLine("Addition : {0} ", c);
        }
        static void Main(string[] args)
        {
            Program obj = new Program();
            obj.add(10, 111);
            Console.ReadLine();
        }
    } 

I am not able to understand why the above Abstract class is not executing ? the above code runs perfect which produces output also. My question is even after overriding the abstract method in the Program class still when calling add() method why the non abstract method is executing and abstract method is not ?

Also If I change the program to :

 abstract public class test
{
    abstract public void add(int a, int b);

}

class Program : test
{
    public void add(decimal a, decimal b)
    {
        decimal c = a + b;
        Console.WriteLine("Decimal Addition : {0}", c);
    }

    public override void add(int a, int b)
    {
        int c = a + b;
        Console.WriteLine("Addition : {0} ", c);
    }
    static void Main(string[] args)
    {

        test obj = new Program();
        obj.add(10, 111);
        Console.ReadLine();
    }
} 

Then my Abstract method is executing and giving me desired result. Please explain anyone. what is actually happening in the line below. I know it very basic question but i am bit confused over here. please help ! Thanks in advance.

test obj = new Program();

Solution

  • Thats because any method declared in derive class has more priority than base inherited methods.

    If you want to call add(int a, int b) you will need to tell the compiler that add(decimal a, decimal b) does not exist. This is by casting it to test

    abstract public class test
    {
        abstract public void add(int a, int b);
    
    }
    
    class Program : test
    {
        public void add(decimal a, decimal b)
        {
            decimal c = a + b;
            Console.WriteLine("Decimal Addition : {0}", c);
        }
    
        public override void add(int a, int b)
        {
            int c = a + b;
            Console.WriteLine("Addition : {0} ", c);
        }
        static void Main(string[] args)
        {
            test obj = new Program();
            obj.add(10, 111);
            Console.ReadLine();
        }
    } 
    

    If you don't want to cast it, you can declare add(decimal a, decimal b); into the abstract class. Like so:

    abstract public class test
    {
        abstract public void add(int a, int b);
    
        abstract public void add(decimal a, decimal b);
    }
    
    public class Program : test
    {
    
        public override void add(int a, int b)
        {
            int c = a + b;
            Console.WriteLine("Addition : {0} ", c);
        }
        public override void add(decimal a, decimal b)
        {
            decimal c = a + b;
            Console.WriteLine("Decimal Addition : {0}", c);
        }
        public static void Main(string[] args)
        {
            Program obj = new Program();
            obj.add(10, 111);
            Console.ReadLine();
        }
    }