public static void Demo()
        {
            Console.WriteLine($"Simple Inheritance");
            Console.WriteLine();

            BaseClass parent = new BaseClass();

            parent.Method1();
            parent.Method2();

            ChildA childA = new ChildA();

            childA.Method1();
            childA.Method2();
            childA.Method3();

            ChildB childB = new ChildB();

            childB.Method1(); //method hiding
            childB.Method2(); //method override

            BaseClass parent2 = (BaseClass)childB;

            parent2.Method1(); // calling BaseClass method
            parent2.Method2(); // derived method will run since it overrides BaseClass

            Console.WriteLine($"------------------------");
            Console.WriteLine();
        }
        public static void Demo()
        {
            Helpers.DisplaySectionTitle("Simple Inheritance");

            BaseClass parent = new BaseClass();

            parent.Method1();
            parent.Method2();

            ChildA childA = new ChildA();

            childA.Method1();
            childA.Method2();
            childA.Method3();

            ChildB childB = new ChildB();

            childB.Method1(); //method hiding
            childB.Method2(); //method override

            BaseClass parent2 = (BaseClass)childB;

            parent2.Method1(); // calling BaseClass method
            parent2.Method2(); // derived method will run since it overrides BaseClass

            Helpers.DisplaySectionEnd();
        }
        public static void Demo()
        {
            Console.WriteLine($"Interface Inheritance");
            Console.WriteLine();

            {
                Console.WriteLine($"Type is class");
                Console.WriteLine();

                ChildA childA = new ChildA();
                childA.Method1();

                ChildB childB = new ChildB();
                childB.Method2();

                ChildAB childAB = new ChildAB();
                childAB.Method1();
                childAB.Method2();
            }

            {
                Console.WriteLine();
                Console.WriteLine($"Type is interface");
                Console.WriteLine();

                IInterfaceA childA = new ChildA();
                childA.Method1();

                IInterfaceB childB = new ChildB();
                childB.Method2();

                IInterfaceA childAB1 = new ChildAB();
                childAB1.Method1();
                // childAB.Method2(); cannot access Method2 because it is not defined in IInterfaceA

                IInterfaceB childAB2 = new ChildAB();
                // childAB2.Method1(); cannot access Method1 because it is not defined in IInterfaceB
                childAB2.Method2();
            }

            Console.WriteLine($"------------------------");
            Console.WriteLine();
        }
Exemplo n.º 4
0
        public static void Demo()
        {
            Helpers.DisplaySectionTitle("Sealed");

            BaseClass parent = new BaseClass();

            parent.Method1();
            parent.Method2();

            ChildA childA = new ChildA();

            childA.Method1();
            childA.Method2();

            ChildB childB = new ChildB();

            childB.Method1();
            childB.Method2(); // ChildA.Method2() will be shown since it cannot be further overriden

            Helpers.DisplaySectionEnd();
        }
Exemplo n.º 5
0
        public static void Demo()
        {
            Console.WriteLine($"Sealed");
            Console.WriteLine();

            BaseClass parent = new BaseClass();

            parent.Method1();
            parent.Method2();

            ChildA childA = new ChildA();

            childA.Method1();
            childA.Method2();

            ChildB childB = new ChildB();

            childB.Method1();
            childB.Method2(); // ChildA.Method2() will be shown since it cannot be further overriden

            Console.WriteLine($"------------------------");
            Console.WriteLine();
        }