Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
class Program
    {
        static void Main(string[] args)
        {
            IInterface interfaceFromBase = new Base();

            Console.WriteLine(interfaceFromBase.test());

            Console.WriteLine(interfaceFromBase.test1());



            IInterface interfaceFromDerived = new Derived();

            Console.WriteLine(interfaceFromDerived.test());

            Console.WriteLine(interfaceFromDerived.test1());



            Console.ReadLine();
        }
    }

    interface IInterface
    {

        string test();

        string test1();

    }



    class Base : IInterface
    {

        public string test()
        {

            return "Test: I'm in Base";

        }



        public string test1()
        {

            return "Test1: I'm in Base";

        }

    }

    class Derived : Base ,IInterface
    {

        public new string test()
        {

            return "Test: I'm in Derived";

        }

    }


The output of the above function is
Test: I'm in Base
Test1: I'm in Base
Test: I'm in Derived
Test1: I'm in Base

If the Derived class is to be derived from Base only then the output would be
Test: I'm in Base
Test1: I'm in Base
Test: I'm in Base
Test1: I'm in Base

Would anyone please give me the difference?
Posted

1 solution

you are experiencing the difference between new and override. BTW: The latter requires the base class method to be declared virtual.

I suggest you read up on all these keywords.

:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900