none
Can we change the return type of a method during overriding in c# RRS feed

Answers

  • Hi,

    You cannot change the return type of method during overriding. If you do so, you will get the below error.

    Error:

    'Sample.Square.Area()': return type must be 'int' to match overridden member 'Sample.ShapesClass.Area()'

    SampleCode to generate <g class="gr_ gr_57 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="57" id="57">error</g>:

    namespace Sample
    {
        abstract class ShapesClass
        {
            abstract public int Area();
        }
        class Square : ShapesClass
        {
            int side = 0;
    
            public Square(int n)
            {
                side = n;
            }
            // Area method is required to avoid
            // a compile-time error.
            public override string Area()
            {
                return "Test";
            }
    
            static void Main()
            {
                Square sq = new Square(12);
                Console.WriteLine("Area of the square = {0}", sq.Area());
                Console.ReadLine();
            }
    
            interface I
            {
                void M();
            }
            abstract class C : I
            {
                public abstract void M();
            }
    
        }
    }



    SRIRAM

    Tuesday, September 26, 2017 7:55 AM

All replies

  • Hi,

    You cannot change the return type of method during overriding. If you do so, you will get the below error.

    Error:

    'Sample.Square.Area()': return type must be 'int' to match overridden member 'Sample.ShapesClass.Area()'

    SampleCode to generate <g class="gr_ gr_57 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="57" id="57">error</g>:

    namespace Sample
    {
        abstract class ShapesClass
        {
            abstract public int Area();
        }
        class Square : ShapesClass
        {
            int side = 0;
    
            public Square(int n)
            {
                side = n;
            }
            // Area method is required to avoid
            // a compile-time error.
            public override string Area()
            {
                return "Test";
            }
    
            static void Main()
            {
                Square sq = new Square(12);
                Console.WriteLine("Area of the square = {0}", sq.Area());
                Console.ReadLine();
            }
    
            interface I
            {
                void M();
            }
            abstract class C : I
            {
                public abstract void M();
            }
    
        }
    }



    SRIRAM

    Tuesday, September 26, 2017 7:55 AM
  • Hi eswar balisetty,

    If your issue is solved please Mark as answer or Vote as helpful post to the appropriate answer so that it will help other members to find solution if they faces similar issue.

    Your understanding and cooperation will be grateful.

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, October 2, 2017 11:42 AM
    Moderator