Inheritance relationship between parent and child classes in C#

Original Link: http://www.cnblogs.com/mingmingruyuedlut/archive/2011/09/15/1874204.html

The inheritance relationship between parent and child classes in C# is similar to that in C and C++. First, the most important point is explained here: if a child class inherits a parent class, then the child class can be casted to the parent class, and the compilation and run are guaranteed to be correct; however, when a parent class is casted to a child class, compilation can run smoothly..See the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FatherAndSon
{
    class ClassA
    {
        static void Main(string[] args)
        {
            //Compile and run can pass
            ClassB b = new ClassB();
            ClassA a = (ClassA)b;
           
            //Compilation will pass, but run smoothly
            ClassA aa = new ClassA();
            ClassB bb = (ClassB)aa;
        }

    }

    class ClassB : ClassA
    {
       
    }
}

In fact: after casting a subclass to a parent, compilation and execution can also pass when using this parent to cast to another subclass; however, errors can occur when converting a parent directly to a subclass.It's like this: Suppose the parent is a "small basket" with five "apples" and the child is a large basket with five "apples" and five "watermelon"; Force the child to the parent <=>Change the parent's small basket to the large basket, but still only five "apples" (parent's).References point to subclasses, but the parent can only call its own variables and methods, and then use this parent to force conversion to another subclass <=>Load 5 references to subclasses into a large basket <=>Load 5 apples and 5 into a small basket of subclasses <=>Load 5 apples and 5 in a parent basketA reference to a watermelon "given to a subclass (which of course cannot be loaded) is shown in the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FatherAndSon
{
    class ClassC
    {
        static void Main(string[] args)
        {   
            //Cast a subclass into a parent class first, then use this parent to cast to another subclass (via)
            ClassB b = new ClassB();
            ClassA a = (ClassA)b;
            ClassB bbbbb = (ClassB)a;

           
            //Compilation can pass, but run without exception
            ClassA aa = new ClassA();
            ClassB bb = (ClassB)aa;
        }
       
    }

    class ClassA
    {
        
    }
    class ClassB : ClassA
    {
        
    }
}

Another notable feature is that whether a subclass cast is assigned to a parent class or a parent class (forced from a subclass) is cast to a subclass; class objects invoke their variables and functions according to the declared type (subclass or parent), regardless of assignment.See the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FatherAndSon
{
    class ClassC
    {
        static void Main(string[] args)
        {   
            //Cast a subclass into a parent class first, then use this parent to cast to another subclass (via)
            ClassB b = new ClassB();
            b.PrintFunc();
            Console.WriteLine(b.myStr);

            ClassA a = (ClassA)b;
            a.PrintFunc();
            Console.WriteLine(a.myStr);

            ClassB bbbbb = (ClassB)a;
            bbbbb.PrintFunc();
            Console.WriteLine(bbbbb.myStr);
           
            //Compilation can pass, but run without exception
            //ClassA aa = new ClassA();
            //ClassB bb = (ClassB)aa;
            //bb.PrintFunc();
        }
       
    }

    class ClassA
    {
        public string myStr;
        public ClassA()
        {
            myStr = "Hello, This is myStr in ClassA";
        }

        public void PrintFunc()
        {
            Console.WriteLine("This is the Print Function in ClassA");
        }
    }

    class ClassB : ClassA
    {
        public string myStr;
        public ClassB()
        {
            myStr = "Hello, This is myStr in ClassB";
        }
        public void PrintFunc()
        {
            Console.WriteLine("This is the Print Function in ClassB");
        }
    }
}

The output from the above code is:

    This is the Print Function in ClassB

    Hello, This is myStr in ClassB

    This is the Print Function in ClassA

    Hello, This is myStr in ClassA

    This is the Print Function in ClassB

    Hello, This is myStr in ClassB

You can see that instance objects of classes are indeed based on the classes they are declared to be when calling their variables and methods.

Reprinted at: https://www.cnblogs.com/mingmingruyuedlut/archive/2011/09/15/1874204.html

Added by mjax on Mon, 22 Jul 2019 21:06:19 +0300