Inheritance in c#

namespace CSharpBasics
{
    class inheritance_bas
    {
        public virtual string fun1()
        {
            return "base_vir";
        }

        public string fun2()
        {
            return "base_nor";
        }

        protected string fun3() //Object of this class cannot access this object as protected, however child class can
        {
            return "base_fun3";
        }
    }

    class inheritance_der : inheritance_bas
    {
        //With OVERRIDE Keyword Now even base class obj will refer to this method when initialized using new inheritance_der()
        public override string fun1() 
        {
            return "der_vir";
        }

        public new string fun2()
        {
            return "der_nor";
        }

        private new string fun3()
        {
            return "der_fun3";
        }
    }

    class ingeritance_der1 : inheritance_bas
    {
        //With Out OVERIRDE base class will point to its own method (new is added to avoid warnings thats it)
        public new string fun1()
        {
            return "der_vir1";
        }

        public new string fun2()
        {
            return "der_nor1";
        }
    }

    class inheritance_der2 : inheritance_der
    {
        public new string fun1()
        {
            return "der2_fun1";
        }

        //warning of adding a new keyword, as base class contain this defination with private access specifier
        //So that means even private methods also gets inherited into the base class.
        public string fun3()
        {
            return "der2_fun3";
        }
    }
}
Add Comments :

Login

Register