Click here to Skip to main content
15,889,728 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello Friends,

I know there are many information available on internet for Inheritance, Overriding and Shadowing. The difference between them etc. etc.

There are some real life scenario with example also explained very well. After reading some of those i can say :-
Shadowing is giving a new life to existing items.
Overriding is changing existing life.

To understand it clearly i wrote a simple test program.
C#
public class cls1
    {
        public void TestFoo() { Console.Write("Base Class TestFoo. \n"); }
        public virtual void fun1()
        {
            Console.Write("cls1 fun1 \n");
        }
        public void foo() { Console.Write("Base class function.\n"); }
    }

    public class cls2 : cls1
    {
        public void TestFoo() { Console.Write("Child Class TestFoo. \n"); }
        public override void fun1()
        {
            Console.Write("cls2 fun1 \n");
        }
        public new void foo() { Console.Write("Child class function.\n"); }
    }

   static void Main(string[] args)
        {
            // Normal

            cls1 objTestFoo = new cls1();
            objTestFoo.TestFoo(); // "Base class TestFoo"

            cls1 objTestFoo1 = new cls2();
            objTestFoo1.TestFoo(); // "Base class TestFoo" ???? Why it is calling Base class function
 
            cls2 objTestFoo2 = new cls2();
            objTestFoo2.TestFoo(); // "Child class TestFoo"           

            // overide

            cls1 obj1 = new cls1();
            obj1.fun1(); // "cls1 fun1"

            cls1 obj2 = new cls2();
            obj2.fun1(); // "cls2 fun1" ????? why it is calling child class fun1

            cls2 obj3 = new cls2();
            obj3.fun1(); // "cls2 fun1"
            
            // new 

            cls1 obj1_new = new cls1();
            obj1_new.foo(); // "Base Class function"

            cls1 obj2_new = new cls2(); ???? why it is calling base class function
            obj2_new.foo(); // "Base Class function"

            cls2 obj3_new = new cls2();
            obj3_new.foo(); // "Child Class function"
}

What i am not able to understand is why it is calling different functions when we give reference of base class in to child class object.
Posted
Updated 9-Mar-14 7:56am
v4

1 solution

It's just for illustration.
It doesn't really work as an example is you have
C#
BaseClass bc = new BaseClass();
bc.MyMethod();
DerivedClass dc = new DerivedClass();
dc.MyMethod();
Giving:
BaseClass MyMethod called
DerivedClass MyMethod called
Because you could get that result without deriving any class at all!
If you write the above as
C#
BaseClass example = new BaseClass();
example.MyMethod();
example = new DerivedClass();
example.MyMethod();
And it gives the same result (which is does) that that shows that inheritance works (a derived class instance reference can be assigned to a base class variable) and that the system sorts out the overriding for you by calling the appropriate method for the actual instance without referring to the variable type.
When you do the same with new methods it does look at the variable type:
C#
Base bc = new Base();
Derived dc = new Derived();
bc.MyMethod();
dc.MyMethod();
bc = dc;
bc.MyMethod();
And you get:
Base: MyMethod called
Derived: MyMethod called
Base: MyMethod called
which shows it.
 
Share this answer
 
Comments
girishmeena 9-Mar-14 14:01pm    
Hello Griff, Thanks for detailed answer. but consider me pro, I have also updated my question.
girishmeena 6-Aug-15 7:50am    
It means, in case of normal and new scenario it looks for variable type. and in case of overriding it looks for instance type.
OriginalGriff 6-Aug-15 8:05am    
If you mean objTextFool.TestFoo() then it's because the variable is a base class variable (containing a derived class instance) and the method is not overridden in the derived class. It's shadowed - which means that it "hides" the base class implementation - but you access the method via a base class variable, so you get the base class implementation. If you do this:

cls2 o2= new cls2();
cls1 o1 = o2;
o1.TestFoo();
o2.TestFoo();

It will execute the base and then the derived class implementation.
Only for overridden methods will it search "up the chain" to find the highest implementation for the actual instance.

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