What is Polymorphism?
Polymorphism is the ability to use a class as more than one type. The class can use a derived type or its own type. The most common use of polymorphism is redefining a method; this can be accomplished by either replacing the members of the base class with the derived class’ members or overriding the members of the base class.
To make this a little more clear I will break down this definition into more manageable terminology. You basically use polymorphism every time you create an object that uses the members of a class (such as in previous blogs I had created objects that implemented members of the Base_Patient and Derived_Patient classes). Microsoft has stated that all objects in C# are an instance of polymorphism. The basic idea is that you are allowing the derived class or object to implement methods, properties and events in manners different than that of which the base class already implements them.
How do I use polymorphism?
An example of using polymorphism would be if in a doctor’s office they had wanted to track the distance of walking a patient could do within a given time frame and then compare this to an average distance of other patients within the same age span.
To begin we would create a class that sets the properties that all patients have in common. We know that all patients have a first name, last name and age. In this sample I had created a class called “Base_Patient” and placed the following code in it:
using System; namespace polymorphism_1 public int intAge; //default constructor for base class //We create the constructor requiring the values of //destructor of object class //We set our property accessors as private so we may //property firstName of patient //property lastName of patient //property Age of patient //We declare our methods as public so our derived objects public void DisplayAge() //You will notice the method to display the }//ends the class |
Now that we have our base class we can create our derived class. In this sample I created a class called “Derived_Patient” and placed the following code in it:
using System; namespace polymorphism_1 //We create a constructor that polymorphs the base //Here we override our base class' "RateOfWalk" method, thus //Here we are calculating the rate of walk for the patient }//ends the class |
Now we create our “Program.cs” file and place the following code in it:
using System; namespace polymorphism_1 //The following lines of code will call the polymorphed and unchanged }//ends the class |
You can see from above that polymorphing had happened intentionally, by creating a method for the “RateOfWalk” that was originally declared in the base class and later used in the derived class. Another instance of polymorphism, although much more subtle, is when we created our constructor of the derived class. The base class contained a property for the patient’s age; however, the constructor of the base class did NOT set this property. When we created the constructor of the derived class we had added the requirement of the patient’s age, thus creating a polymorphed constructor (albeit unconventional).
The calculations used in the “RateOfWalk” method in the derived class may not be practical (I chose this method so I can also demonstrate the use of an “if” statement), and especially not clear cut; although it should show clearly that you can create different implementation code for methods declared in other base classes. Don’t get hung up on what the code is calculating or why it is; just remember that we did not have any implementation code of the method in the base class and we were able to create the implementation code in the derived class!
Keep in mind this blog is only introducing you to the concept of “polymorphism”, there are many more features and rules than what I can cover. There are many resources available; books have chapters discussing this, websites have sections and forums discussing this and you will come across it in many examples (rather you realize it or not). The best starting point I can give you, without requiring the purchasing of materials, is to refer you to Microsoft’s website discussing “polymorphism”. It is a decent starting point; however, it does get very technical. Google is another good way to find out more information; do be aware that “polymorphism” is slightly different in programming languages, so you should ensure that you are reading material related to C# programming.
Happy coding!
No comments:
Post a Comment