Friday 2 December 2016

Storing child class objects in a List of base class

abstract class A
    {
        public abstract void diap();
    }
    class B:A
    {
        public override void diap()
        {
            Console.WriteLine("B");
           
        }
    }
    class C:B
    {
        public override void diap()
        {
            Console.WriteLine("C");
            base.diap();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<A> li = new List<A>();

            B obj1 = new B();
            C obj2 = new C();

            li.Add(obj1);
            li.Add(obj2);
           
            foreach(A ob in li)
            {
                ob.diap();
            }
            Console.ReadLine();
        }

    }

Output - 
B
C
B

No comments:

Post a Comment

Note: only a member of this blog may post a comment.