C#, CodeProject, OOP

Polymorphism: Empowering your objects

Hi there readers, So, we’re finally getting into the final pilar of object oriented programming, polymorphism. It’s a strange word at first, but we get use to it pretty fast.

What does the word polymorphism mean?

The root of polymorphism is traced back to the greeks. Poly means many and morphism mean forms. Basically, polymorphism gives an object the ability of having multiple forms. The fact that an object can have multiple forms means that while using inheritance of dependency injection (an advance topic which will be mentioned later), we don’t have to care what kind of object we’re calling during runtime. Having multiple forms means that while calling a certain function, in the hierarchy of objects, custom or not, it won’t matter which objects is being called. A real world example of polymorphism

Example

Everyone is a person, but each one as his own waking up routine at a specific time of the day:

  • As a waiter
  • As a lawyer
  • As a student taking classes at nights

Method overriding? Virtual methods? What are those ??

Basically, virtual methods are found in the parent class. Virtual methods are behaviors in parent objects which can be overridden by their children. To make sure a method can be use by the children and be overridden, not only it must have the virtual keyword in its signature, but it should also have the protected keyword to make sure that only children can use it. This is a key concept in oriented object programming.

Easily, it’s possible to see why such a feature can be attractive for programmers. We love code reuse and we hate rewriting the same piece of code. Some readers might wonder why we hate rewriting code, right ? Well, it’s pretty simple in fact. It’s a bad programming behavior, YOU SHOULD NOT DO THAT ! Having to rewrite code will double or even triple or X factor time the amount of time you need to invest into maintaining the software.

Back to virtual methods. Having a basic foundation for most of the component in your system simplifies the work that must be completed. More than just this, when you’re overriding behaviors, it’s because you have a darn god reason to do this. in fact, while you’re testing your code or going over your software a few months after writing it, you’ll understand exactly why you did why you did. We usually do this because we have a good reason why.

public abstract class Human
{
   protected virtual void Greetings(string name)
   {
       Console.WriteLine("Hi, my name is {0}:", name);
   }
}

public class Doctor : Human
{
   public void GreetingPatient(string patientName,string doctorName)
   {
     Console.WriteLine("Hello {0}",patientName);
     Greetins(doctorName);
   }
   protected override void Greetings(string name)
   {
     Console.WriteLine("Hi, I'm doctor {0}. It's nice to meet you.",name);
   }
 }
Advertisements
Tagged

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s