c inheritance and polymorphic use example

  • 2020-06-01 10:52:07
  • OfStack

Inheritance and polymorphism

A derived class has all non-private data and behavior of the base class and all other data or behavior defined by the new class itself, that is, a subclass has two valid types: the type of the subclass and the type of the base class it inherits.

The ability of an object to represent multiple types is called polymorphism.

Polymorphism example


public class Parent
    {
        public Parent() { }
        public void MethodA()
        {
            Console.WriteLine(" call MethodA()");
        }
        public class Child:Parent
        {
            public Child() { }
            public void MethodB()
            {
                Console.WriteLine(" call MethodB()");
            }

        }
        class Test 
        {
            static void Main(string[] args)
            {
                Parent oParent = new Parent();
                oParent.MethodA();//OK  Call type Parent Member method of 
                //Child oChild1 = (Child)oParent;// Runtime error 
                Child oChild=new Child();
                oChild.MethodB();//OK  Calling a derived class Child Member method of 
                oChild.MethodA();//OK  Call the base class Parent Member method of 
                Parent oParent1 =(Parent)oChild;
                oParent1.MethodA();
                //oParent1.MethodB();// Compile error, type Parent There is no method MethodB()
                Console.ReadLine();
                Child oChild1 = (Child)oParent1;
                oChild1.MethodB();//OK  Calling a derived class Child Member method of 
                oChild1.MethodA();//OK  Call the base class Parent Member method of 
                Console.ReadKey();
            }
        }

    }

In the above example, class Child is both valid Child and valid Parent. oChild can be used as the type Child, so it has the method MethodB() defined by Child itself and the method MethodA() defined by the base class Parent. At the same time, object oChild can also be cast to Child's base class Parent's object oParent. Casting does not change the content of the oChild object, but the oParent object is of type Parent and therefore has only the method MethodA() defined by class Parent.

After casting Child to Parent, the Parent can be cast back to Child. And only those instances that are actually instances of Child can be cast to Child, otherwise there will be a run error: an object of type Parent cannot be cast to type Child.

Inherited type

C# contains two types of inheritance: implementation inheritance and interface inheritance.

Inheritance now represents that one type is derived from one base type, and the derived class has all the non-private (non-Private) data and behavior of the base class. In implementation inheritance, each method of a derived type takes the implementation code of the base type, unless the implementation code that overrides the method is specified in the definition of the derived class. Implementation inheritance 1 is generally used to add functionality to an existing type, or many related types share a set of important common functions.

Interface inheritance means that one type implements several interfaces, and the interface only contains the signature of the method, so interface inheritance does not inherit any implementation code. Interface inheritance 1 is generally used to specify that the type has a class of available properties; for example, if the specified type is derived from interface System.IDisposable and implements Dispose (), the method to clean up the resources for the IDisposable interface, it can be called through workpass's mechanism to clean up the resources. Since the way the resource is cleaned up is specific to different types, it does not make sense to define generic implementation code in the interface, which is the contract, and the type derived from the interface, which guarantees that the class provides the functionality specified by the interface.


Related articles: