Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Polymorphism in C# [Object-oriented Programming]

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.

This is brilliant. I like how you used animals as the forms and this is very informative. Now Skid's can shut up about Polymorphic hax and learn what it actually means in OOP. Rated 5.
Five stars Perplexity good job. Make sure to submit it on the competition thread.
(Feb 1, 2013 04:40 PM)tweety Wrote:  This is brilliant. I like how you used animals as the forms and this is very informative. Now Skid's can shut up about Polymorphic hax and learn what it actually means in OOP. Rated 5.

Thanks, Tweety; it really means a lot that you appreciate my tutorial.
Animals were the only things I could think of at the time, I could have used something else but I figured that would be the best one.

(Feb 1, 2013 04:42 PM)+Bourbon Wrote:  Five stars Perplexity good job. Make sure to submit it on the competition thread.

Thanks a lot, it's nice to have a great programmer like you to give positive feedback! c:
And yeah, I'll do that now in fact.
Pretty basic, yet I believe that it will be useful to beginners. Good job :)
(Feb 1, 2013 04:45 PM)pr3fix Wrote:  Pretty basic, yet I believe that it will be useful to beginners. Good job :)

It is a pretty easy concept to understand, I just struggled putting it into words so I'm glad you think I did a good enough job, thanks a lot :3
(Feb 1, 2013 04:40 PM)tweety Wrote:  Now Skid's can shut up about Polymorphic hax and learn what it actually means in OOP. Rated 5.

People are still using the proper definition, they're just referring to Polymorphic Encryptions.
(Feb 1, 2013 04:45 PM)pr3fix Wrote:  Pretty basic, yet I believe that it will be useful to beginners. Good job :)

Translation: I'm really good at C# and I want you all to know that I already know how to do this.

Thanks for the tutorial OP.
(Feb 1, 2013 04:54 PM)Koolg223 Wrote:  Translation: I'm really good at C# and I want you all to know that I already know how to do this.

Thanks for the tutorial OP.

Na. I'm actually glad that people are actually taking their time to make tutorials, has nothing to do with me.
(Feb 1, 2013 04:54 PM)Koolg223 Wrote:  Translation: I'm really good at C# and I want you all to know that I already know how to do this.

Thanks for the tutorial OP.

This is one of the most basic things someone should learn. If you don't know how to do this and you say you can code in C#, then there is something wrong with you.
(Feb 1, 2013 05:02 PM)Occulta Wrote:  This is one of the most basic things someone should learn. If you don't know how to do this and you say you can code in C#, then there is something wrong with you.

And you've missed the point entirely...

Also, you're wrong, there are many far more basic things. If you polled this forum, I'm guessing at least 50% of people here don't know how to use it.
This is a very good tutorial. Incredible job!

Kik - Krookedlabratories
(Feb 1, 2013 05:34 PM)зяяоя Wrote:  This is a very good tutorial. Incredible job!

Thanks a lot for the positive feedback, I appreciate it! :3
Great tut from a great coder! <3 i luf u
Wow! Almost half the threads in the .NET section gave me cancer, and this thread has cured me. Thanks.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by Russian bearFeb 21, 2022 08:06 PM 3 240 Feb 21, 2022 08:06 PM
Last Post: MavenGradle
[C#] by NvikodvJan 18, 2019 06:43 PM 0 159 Jan 18, 2019 06:43 PM
Last Post: Nvikodv
[Java] by deadsh0tJan 10, 2019 07:30 AM 2 170 Jan 10, 2019 07:30 AM
Last Post: deadsh0t