logo

C# How to create different concrete classes using polymorphism

Overview: This code sample demonstrates how to implement polymorphism in c#. Polymorphism is one of the three pillars of object orient programming. Polymorphism means several forms. A dog is an mammal. The mammal class is defined as abstract meaning it can not be instantiated. However, the properties and methods of the abstract class can be accessed by the derived concrete class. An abstract class instance can reference a derived concrete class, but a concrete class can not reference an abstract class. Mammal mammal=dog is allowed but Dog dog=mammal is not.

Polymorphism can be implemented by abstract GetSound method with the override GetSound method in the Dog class. Additionally, the Dog class constructor can be have multiple parameter types allowing for different behavior and attributes of the concrete class.

The ICollection or ICollection<T> can be used to reference a list of strings or a list<T> types. ICollection is often used by entity framework.

The dynamic key word is used to create an anonymous class using a key/value pair combination of fieldname and value. The type is determined by the compiler.

Mammal Output:

	Mammal Warm Blooded=true

	Mammal Attributes:
	Warm Blooded
     	Suckles their Young 

Dog Output:
	Dog sound=Bark
	Dog Pack Hunter=true
	Dog is a puppy=true


	
Is A:
	Breed="German Sheppard"
	Sex="Female"
	Color="Brown"
	Age=2
Has A:
	Tail, Loud Bark, Soft Fur

Code


Dog dog = new Dog(new
                {
                    Breed = "German Sheppard",
                    Sex = "Female",
                    Color = "Brown",
                    Age=1
                }
                ,
                new List<string>(new string[] {
                "Tail",
                "Two Ears",
                "Loud Bark",
                "Soft Fur"
                })
                );
                
                //Dog dog = new Dog();

                MessageBox.Show("Mammal Warm Blooded=" + dog.IsWarmBlooded().ToString());
		Mammal mammal = dog;
                string mammalAttributes = "";
                foreach (var item in mammal.IsA)
                {
                    mammalAttributes += item + "\n";
                }
		MessageBox.Show("Mammal attributes\n" + mammalAttributes);
                MessageBox.Show("Dog sound=" + dog.GetSound());
                MessageBox.Show("Dog Pack Hunter=" + dog.IsAPackHunter().ToString());
                MessageBox.Show("Dog is a puppy=" + dog.IsAPuppy().ToString());

                string IsABuffer = "";
                dynamic IsA = dog.IsA;
                IsABuffer +="Breed="+IsA.Breed + "\n";
                IsABuffer += "Sex=" + IsA.Sex + "\n";
                IsABuffer += "Color=" + IsA.Color + "\n";
                IsABuffer += "Age=" + IsA.Age + "\n";

                MessageBox.Show("Is a \n" + IsABuffer);
                string HasA = "";
                foreach (var item in dog.HasA)
                {
                    HasA += item + "\n";
                }
                MessageBox.Show("Has a \n" + HasA);
            

Abstract Class Mammal


 public abstract class Mammal
    {
        public bool IsWarmBlooded()
        {
            return true;
        }
        public List<string> IsA = new List<string>(new string[] {"Warm Blooded",
     "Suckles their Young" });
     
        public abstract string GetSound();
    }

Concrete Class Dog

  public class Dog : Mammal
    {
        object _IsA;
        List<string> _HasA;
        public Dog()
        {
            _IsA = new
            {
                Breed = "Unknown",
                Sex = "Unknown",
                Color = "Unknown",
                Age = -1
            };

            _HasA = new List<string>(new string[] {
                "Tail",
                "Two Ears",
                "Bark",
                "Fur"
                });
            IsA = _IsA;
            HasA = _HasA;
        }
        public Dog(dynamic IsAttributes, dynamic HasAAttributes)
        {
            _IsA = IsAttributes;
            _HasA = HasAAttributes;
            IsA = _IsA;
            HasA = _HasA;
        }
        public override string GetSound() { return "Bark"; }
        public bool IsAPackHunter() { return true; }
        public bool IsAPuppy()
        {
            dynamic dogIsA = IsA;
            if (dogIsA.GetType().GetProperty("Age")!=null)
            {
                if (dogIsA.Age < 1 && dogIsA.Age!=-1)
                {
                    return true;
                }
            }
            return false;
        }
        public object IsA = null;

        public ICollection HasA = null;

    }
s