Example #1
0
        public Asclasseg()
        {
            /*
             * Upcasting.
             * Upcasting is when we assign a variable with base
             * type the value (another variable) of the subclass
             * type, which implictly (automatically) converts the
             * value (of sublclass type) to base class type, then
             * assigns it to the base class variable.
             *
             * E.g
             * Subclass s1 = new Subclass();
             * Baseclass b1 = s1; // implictly upcast.
             * */

            /* Even with the upcasting, the upcasted variable will
             * create a new reference that point to the Sublcass
             * object, thus it will use the subclass versions of
             * the class members (variables and methods).
             * */

            InheritClass2 c2Obj = new InheritClass2("Upcasting");
            InheritClass1 c1Obj = c2Obj;             // implict upcast

            /*
             * Downcasting
             * Downcasting is when we assign a variable with subcl-
             * ass type the value (another variable) of the base
             * class type, which explictly converts the value (of
             * base class type) to the subclass type, then assigns
             * it to the subclass variable.
             *
             * E.g
             * Subclass s1 = new Subclass();
             * Baseclass b1 = s1; // implictly upcast.
             * Sublcass s2 = (Basclass)b1; // explict downcast.
             * */

            InheritClass2 c2 = new InheritClass2("Upcasting");
            InheritClass1 c1 = c2;                // implicit upcast
            InheritClass2 c3 = (InheritClass2)c1; // explicit downcast
            // See here, we have tell the compiler explicitly to
            // convert c1 and return a subclass type.
        }
 public static void OvaLoad(InheritClass1 inclass1)
 {
     Console.WriteLine("OvaLoad Base");
     inclass1.Display();
 }