Feb 1, 2013 04:30 PM
(This post was last modified: Feb 6, 2013 04:22 AM by Perplexity.)
Introduction
A lot of people get confused by the term "Polymorphism", Polymorphism can mean a variety of things, it all depends on the context in which you use it. In this example, I'm going to be talking about Polymorphism in Object-oriented programming.
Let's take a look at the word to start with, Polymorphism.
Poly means many, and morph is to do with forms. So basically, Polymorphism in object-oriented programming is when an object can have more than one form.
The word Polymorphism comes from the Greek word that means "many shapes".
Polymorphism is helpful for when you have many object types that aren't exactly related but share a common interface. (For example, every animal makes a noise but not all of them make the same noise nor share the same type of habitat or behave in the same way.)
Let's get started
Using the example I gave above with the animals, we're going to create a polymorphic object type called Animal, this is going to be what's known as our base class. Once we've got our base class, we'll create a few other classes which derive from our base class via inheritance, these are known as our derived classes.
So let's start with our base class; Animal:
[img]http://i.hfperplexity.info/5488ruxtmvwi.png[/img]
As you can see, we have our makeNoise() method in which all animals will have.
Any other class we create now which derives from this class will be able to perform this method using the override modifier, this is needed in order to extend or modify the implementation of an inherited method.
So let's go ahead and create another class called Dog which derives from our base class, Animal:
[img]http://i.hfperplexity.info/hcmp8tq9.png[/img]
Now we can implement this into action in our main application like so:
[img]http://i.hfperplexity.info/roxd6atdz.png[/img]
This will output the following:
[img]http://i.hfperplexity.info/urvrqpgyqoo.png[/img]
But of course, we are not limited to just one type; let's do another example but using more derived types:
[img]http://i.hfperplexity.info/vpg4cqat52db21x.png[/img]
And if you've been paying attention up until now, I'm sure you can guess what the code above will output; I'll go ahead and show you anyway:
[img]http://i.hfperplexity.info/g2qfclvqej0m1.png[/img]
And of course, there will be times where a derived class will have its own exclusive methods or properties, and therefore would require a type cast in order to be used; let me give you an example using the Cat class:
[img]http://i.hfperplexity.info/1n3cwp60.png[/img]
And like I said, that method is exclusive to the Cat class and can only be used when the object has been casted to the appropriate type:
[img]http://i.hfperplexity.info/a5o9ndbgo3e9f.png[/img]
Thank you for reading
This has been my tutorial covering Polymorphic Object Referencing in C#. I hope some of you have found this useful and if you have any questions regarding this tutorial, feel free to ask.
And of course, if you have any criticism or suggestions then let me know and I'll try and fix it up.
A lot of people get confused by the term "Polymorphism", Polymorphism can mean a variety of things, it all depends on the context in which you use it. In this example, I'm going to be talking about Polymorphism in Object-oriented programming.
Let's take a look at the word to start with, Polymorphism.
Poly means many, and morph is to do with forms. So basically, Polymorphism in object-oriented programming is when an object can have more than one form.
The word Polymorphism comes from the Greek word that means "many shapes".
Polymorphism is helpful for when you have many object types that aren't exactly related but share a common interface. (For example, every animal makes a noise but not all of them make the same noise nor share the same type of habitat or behave in the same way.)
Let's get started
Using the example I gave above with the animals, we're going to create a polymorphic object type called Animal, this is going to be what's known as our base class. Once we've got our base class, we'll create a few other classes which derive from our base class via inheritance, these are known as our derived classes.
So let's start with our base class; Animal:
[img]http://i.hfperplexity.info/5488ruxtmvwi.png[/img]
As you can see, we have our makeNoise() method in which all animals will have.
Any other class we create now which derives from this class will be able to perform this method using the override modifier, this is needed in order to extend or modify the implementation of an inherited method.
So let's go ahead and create another class called Dog which derives from our base class, Animal:
[img]http://i.hfperplexity.info/hcmp8tq9.png[/img]
Now we can implement this into action in our main application like so:
[img]http://i.hfperplexity.info/roxd6atdz.png[/img]
This will output the following:
[img]http://i.hfperplexity.info/urvrqpgyqoo.png[/img]
But of course, we are not limited to just one type; let's do another example but using more derived types:
[img]http://i.hfperplexity.info/vpg4cqat52db21x.png[/img]
And if you've been paying attention up until now, I'm sure you can guess what the code above will output; I'll go ahead and show you anyway:
[img]http://i.hfperplexity.info/g2qfclvqej0m1.png[/img]
And of course, there will be times where a derived class will have its own exclusive methods or properties, and therefore would require a type cast in order to be used; let me give you an example using the Cat class:
[img]http://i.hfperplexity.info/1n3cwp60.png[/img]
And like I said, that method is exclusive to the Cat class and can only be used when the object has been casted to the appropriate type:
[img]http://i.hfperplexity.info/a5o9ndbgo3e9f.png[/img]
Thank you for reading
This has been my tutorial covering Polymorphic Object Referencing in C#. I hope some of you have found this useful and if you have any questions regarding this tutorial, feel free to ask.
And of course, if you have any criticism or suggestions then let me know and I'll try and fix it up.




![[Image: kixHQQg.jpg]](https://i.imgur.com/kixHQQg.jpg)