Latest Posts

Saturday, May 20, 2017

Dynamic/Runtime Polymorphism / Object Slicing C#.net



Few days back  One of my fried asked me to help him in hiring C# resources. I asked about the requirement. He told me , he is looking for people having experience of 3 to 5 yrs in C# development. 

I prepare a list of basic questions mostly on my favorite topic Object oriented programing (OOP's). Initially the interview was smooth. It seem like all the resources have very good understanding of C#. After few interviews I started to do some tongue twisting with OOPS word like instead of asking function overloading/overriding I started asking Dynamic/Run time Polymorphism or Static/Compile time Polymorphism and people were stuck. 

I given them below two class 
====================================================================
public class ParentClass
    {
        public String add(int i, int j)
        {
            return "In Parent class";       
        }

        public String hideParent(int i, int j)
        {
            return "In Parent class";
        }
    }
===================================================================
    public class ChildClass : ParentClass
    {
        public new String  add(int i, int j)
        {
            return "In Child";
        }

        public new String hideParent(int i, int j)
        {
            return "In Child Class With New Keyword";
        }

        public new String onlyInChild(int i, int j)
        {
            return "In Child Class whiout overriding";
        }
    }

==============================================================

And the Questions were what will be the out put in below scenarios

//1. #########################################################################

ParentClass objParentClassWithParentObject = new ParentClass();
Console.WriteLine("Parent class with parent Object :  " + objParentClassWithParentObject.add(3, 5));
//2. #########################################################################
ChildClass objChildClassWithChild = new ChildClass();
Console.WriteLine("Child class with Child Object :  " + objChildClassWithChild.add(3, 5));

//3. #########################################################################

ParentClass objParentClassWithChildObject = new ChildClass();
Console.WriteLine("Parent class with Child Object :  " + objParentClassWithChildObject.add(3, 5));

//4. #########################################################################

//ParentClass objParentClassWithChildObjectNew = new ChildClass();
//Console.WriteLine("Parent class with Child Object :  " + objParentClassWithChildObjectNew .onlyInChild(3, 5));

//5. #########################################################################

//ChildClass objChildClassWithParent = (ChildClass) new ParentClass();
//Console.WriteLine("Child class with Parent Object :  " + objChildClassWithParent.add(3, 5));
 //6. #########################################################################

ParentClass objParentClassWithChildObjectAndNew = new ChildClass();
Console.WriteLine("Parent class with Child Object and New Definition :  " + objParentClassWithChildObjectAndNew.hideParent(3, 5));

//7. #########################################################################

ParentClass objParentClassWithChildObjectAndNew = new ChildClass();
Console.WriteLine("Child class with Child Object and New Definition :  " + objChildClassWithChildObjectAndNew.hideParent(3, 5));

1st and 2nd scenario are very straight forward. We are creating parent object and initializing it with parent class so the result will be  " Parent class with parent Object : In Parent Class"

The child object is created with child class and initialize child class object so the output will be " Child class with Child Object :  In Child "

In 3rd scenario we are creating parent class object and initializing it with child class . Here concept of object slicing will take place.


The child class holds all the behavior and property of parent class so the parent class will get initialized and behavior and property of child class will get discarded . With this call parent class function will get called.

The output will be "Parent class with Child Object :  In Parent Class"

In 4th scenario the concept of object slicing will work but when we try to access the function which is defined in child class only it will give compile time error  "Class doesn't contain definition for the function .....".

In 5th scenario we are trying to initialize child class object with parent class object by  type casting it into child class. It will give error at run time.

In 6th scenario we are creating parent class object and initializing it with child class. The only difference is here the new key word used in function definition in child class .When we use new keyword in child class it gives new definition to the function. But as we are initializing the parent class object the output will be  "Parent class with Child Object :  In Parent Class"


7th scenario it pretty straight forward , we are creating child object and initiliazing it with child class. It donst matter new key word is used or not it will call the child class method. The output will be "Child class with Child Object and New Definition : In Child Class With New Keyword."
 I have tried to cover all scenario, if you have any query or problem feel free to contact me via comment.

1 comment:

  1. thanks you so much for coving all scenario which helps for interview preparation
    but last scenario i guess its typing mistake
    ChildClass objChildClassWithChildObjectAndNew = new ChildClass();
    Console.WriteLine("Child class with Child Object and New Definition : " + objChildClassWithChildObjectAndNew.hideParent(3, 5));

    ReplyDelete