DEV Community

Cover image for CONCEPTS OF OBJECT-ORIENTED PROGRAMMING-POLYMORPHISM
Elegberun Olugbenga
Elegberun Olugbenga

Posted on

CONCEPTS OF OBJECT-ORIENTED PROGRAMMING-POLYMORPHISM

Welcomeee to part 4 and the final episode of our exploration into the concepts of Object-Oriented Programming...Check out the other articles here Inheritance, Encapsulation, Abstraction. In this episode, we are going to be looking at Polymorphism and then I will give a summary of how the 4 concepts merge together.

As usual, we start from the dictionary meaning of the word polymorphism -according to Wikipedia polymorphism is the occurrence of two or more clearly different morphs or forms. What this means in programming is that one entity can have different implementations. In human terms, a person can be a brother, a husband, a CEO, and a Father. The person still has the same name and the same identity but he just performs different roles to different people. He is taking on different forms depending on his Audience. If we use an example from our Social media App we have been building throughout this series. In the Register Class, we have a method to send Emails.

public class Registration
{
   public string UserName {get; set;}
   public string FirstName {get; set;}
   public string sendEmailtoUser ()
   {
          ///logic to send mail.
          return "Email Sent SuccessFully";
   }
}
Enter fullscreen mode Exit fullscreen mode

Let's say that in the UserManagerClass that inherits from the Registration Class we want to implement a special email to be sent to customers who have not used the app in a while and this email should have a different template from the one you inherited from the Registration class. Polymorphism allows you to override the sendEmailtoUser method you created in the Registration class and implement your own custom logic. There are several ways to perform polymorphism in this article we will look at two.

VIRTUAL KEYWORD AND METHOD OVERRIDING

If you make a method virtual you have enabled its logic and functionality to be overridden in any class that derives from it.

Example:

public class Registration
{
   public string UserName {get; set;}
   public string FirstName {get; set;}
   public virtual string sendEmailtoUser ()
   {
          ///logic to send mail.
          return "Email Sent SuccessFully";
   }
}
Enter fullscreen mode Exit fullscreen mode

If we then Inherit from Registration in our UserManager Class

public class UserManager: Registration
{
   public string UserName {get; set;}
   public string FirstName {get; set;}
   public override string sendEmailtoUser ()
   {
           //////special custom logic to send Emails
          /////////The sendEmailtoUser method has been 
          ////////overriden here

          return "Email Sent SuccessFully";
   }
}
Enter fullscreen mode Exit fullscreen mode

If you create an instance of the UserManager class

      var usermanager= new UserManager();
       string =  usermanager.sendEmailtoUser();
Enter fullscreen mode Exit fullscreen mode

It is the custom sendEmailtoUser implementation made for the UserManager class that will be executed. This custom implementation is only available in the UserManager class other classes that inherit from the Registration class will still use the default sendEmailtoUser method Except if they override the method themselves. When a compiler sees a virtual method it checks to see if it has been overridden in any class deriving from that class if it has been overridden it performs the overridden method. If it hasn't it executes the default function.

So the same method can have different logic in the different classes that inherit from it.
Polymorphism enables an action to take on different functionalities based on the class that overrides it.

Method Overloading

Method overloading refers to when two or more methods in a class have the same name but different input and output parameters. Method overloading in C# can be performed by changing the number of arguments, the data types of the arguments or the return data types. It is a form of polymorphism because the same method name can perform different logic depending on the type of parameters passed to it. Let's look at the Registration class sendEmailtoUser function and overload its method.

public class Registration
{
   public string UserName {get; set;}
   public string FirstName {get; set;}
   public string sendEmailtoUser ()
   {
          ///logic to send mail.
          return "Email Sent SuccessFully";
   }
   public string sendEmailtoUser (string emailaddress, string message)
   {
          ///logic to send mail2.
          return "Email Sent SuccessFully";
   }
}
Enter fullscreen mode Exit fullscreen mode

If we look closely there are two methods to send Email. But how will the compiler know which one to run?. The compiler checks for the arguments passed to the method to know which one it should execute.
If we create an instance of the Registration class and do not pass any parameter in the function the first email function will be executed but if we pass parameters like email address and email message to it the second email function will be executed. Look at the figure below.

Method Overloading

Method Overloading on Send Email Method

Polymorphism is useful for code reusability; it helps to reuse fields and methods of an existing class when you create a new class.
Polymorphism allows you to define one interface and have multiple implementations.

To sum it all up.

Inheritance: helps us to pass down properties and fields across classes.
Polymorphism: Polymorphism allows us to perform a single action in different ways.
Abstraction: helps to provide a layer of invisibility between classes and properties. It hides the details of implementation. (detail hiding)
Encapsulation: helps us to know which class or property should be seen or accessed. (information hiding).

Till Next time Happy Coding......

Oldest comments (0)