Research on Inheritance and Polymorphism in C#

Keywords: C# Attribute

Research on Inheritance and Polymorphism in C#

Usually inherited

In this section, I will build two classes A and B, which are subclasses of A.

namespace CSharpPolymorphism {
    class A {
        public string value = "A";
        public string Say() { return "I'm A, My Value: " + value; }
    }
    class B : A {
        public string value = "B";
        public string Say() { return "I'm B, My Value: " + value; }
    }
    class Program {

        static void Main(string[] args) {
            A Aa = new A();
            B Bb = new B();
            A Ab = new B();

            Console.WriteLine(Aa.Say() + ", Got Value: " + Aa.value);
            Console.WriteLine(Bb.Say() + ", Got Value: " + Bb.value);
            Console.WriteLine(Ab.Say() + ", Got Value: " + Ab.value);

            // Pause
            Console.ReadLine();
        }
    }
}

The results are as follows:

// I'm A, My Value: A, Got Value: A
// I'm B, My Value: B, Got Value: B
// I'm A, My Value: A, Got Value: A
  • The results show that I'm N denotes the method that calls N, My Value: N denotes the value obtained inside the method, and Got Value: N denotes the value obtained by accessing object attributes directly from outside.

Thus, in inheritance, if subclasses are used to instantiate a parent object, whether accessed from inside or outside of the class, the object will use the attributes and methods of the parent class.

The parent class defines the virtual method and the child class creates the new method

In this section, I will build two classes A and B, which are subclasses of A. The method in A is defined as virtual method, and the method in B is defined as new method.

namespace CSharpPolymorphism {
    class A {
        public string value = "A";
        public virtual string Say() { return "I'm A, My Value: " + value; }
    }
    class B : A {
        public string value = "B";
        public new string Say() { return "I'm B, My Value: " + value; }
    }
    class Program {

        static void Main(string[] args) {
            A Aa = new A();
            B Bb = new B();
            A Ab = new B();

            Console.WriteLine(Aa.Say() + ", Got Value: " + Aa.value);
            Console.WriteLine(Bb.Say() + ", Got Value: " + Bb.value);
            Console.WriteLine(Ab.Say() + ", Got Value: " + Ab.value);

            // Pause
            Console.ReadLine();
        }
    }
}

The results are as follows:

// I'm A, My Value: A, Got Value: A
// I'm B, My Value: B, Got Value: B
// I'm A, My Value: A, Got Value: A

As in the previous example, when a subclass creates a new virtual method of the parent class, if it uses a subclass to instantiate the parent object, whether accessed internally or externally, the object will use the attributes and methods of the parent class.

Superclass defines virtual methods and subclass overrides methods

In this section, I will build two classes A and B, which are subclasses of A. The method in A is defined as virtual method, and the method in B is defined as override override method.

namespace CSharpPolymorphism {
    class A {
        public string value = "A";
        public virtual string Say() { return "I'm A, My Value: " + value; }
    }
    class B : A {
        public string value = "B";
        public override string Say() { return "I'm B, My Value: " + value; }
    }
    class Program {

        static void Main(string[] args) {
            A Aa = new A();
            B Bb = new B();
            A Ab = new B();

            Console.WriteLine(Aa.Say() + ", Got Value: " + Aa.value);
            Console.WriteLine(Bb.Say() + ", Got Value: " + Bb.value);
            Console.WriteLine(Ab.Say() + ", Got Value: " + Ab.value);

            // Pause
            Console.ReadLine();
        }
    }
}

The results are as follows:

// I'm A, My Value: A, Got Value: A
// I'm B, My Value: B, Got Value: B
// I'm B, My Value: B, Got Value: A

In this example, if you use a subclass to instantiate a parent object, the method of the subclass will be called, and the attribute value obtained in the method is a subclass, but when accessing the attribute of the object directly from the outside, the attribute value of the parent object will be used.

Posted by maralynnj on Thu, 11 Apr 2019 17:27:32 -0700