Object Oriented Programming (OOP) – Overloading methods – Practical example with C#.

Introduction:

As we all know, Object Oriented Programming (OOP) which maps real word concept to software to develop a good product for real scenarios. Some time we come across only definitions for OOP concept, like if I ask what is overloading or polymorphic behavior we will end up saying ‘Writing two methods names same but with different signatures and return types’.

History:

In this article I will explain the same with my real time experience which I had while working on project. It includes scenario where client need few changes for I need to update functionality for existing method in base class.

If matter comes for base class, as we know it will be used by  many child entities, in my case I had same problem, I was auditing customer actions which entity they use more. To achieve this framework had already a method.

Real scenario:

Overloading gives as extension where to achieve the extension for existing functionality without modifying original one. This line I red but I understood it’s reality in my project.

OK, without making much delay let’s jump into the actual scenario. My method for auditing customer/user actions as showing below in Method -1.

Public static bool CustomerActions(Customer cust,other parameters)

{

//code to interact with database.

}

Method – 1  – CustomerActions to save Customer actions.

I got a requirement to achieve one new feature using the same method. To do the same feature I need to do some extra work on same method that is CustomerActions (I mean extra parameter to same method). Let’s say I need to pass list of buttons clicks.

As we discussed, we should not make break the existing child entities which all are using the same base method. So to achieve this we had  a solution that is making a new parameter as optional.

In my case, we made like List<CustomerClicks> = null in the same method. I have shown updated method signatures in Method-2 as shown below.

Public static bool CustomerActions(Customer cust,other parameters,  List<CustomerClicks> =  null )

{

//code to interact with database.

}

Method  2 – CustomerActions with default parameter  List<CustomerClicks> = null.

So with above implementation, we haven’t break existing child entities which already using the same method.  With this we sent installer to testing team (here developer has tested for only few child entities by referring updated base class, so he could not found any issues while unit testing).

In testing phase, we started facing an error saying ‘Method not found with CustomerActions(Customer,other parameters)’. We not able to believe the issues caused for all child entities which we’re referred as base.

When I went deeply, I found that child entities which and all referred this as base class with old version will get fail, why – because as base class has changed his signature (even though it’s a null able parameter), all child entities not got updated because all references not updated with this latest one, those all looking for old implementation of method.

Solution – for this situation solution is like –  We need to manually  needs to update all child entities with latest base class version – which is very tedious and time consuming work.

That time – I just got OOP feature that is overloading. I just created new overload method with new parameter (I created common method for database code sharing – so that we can avoid code redundant. I shown the code implementation in method – 3.

Public static bool CustomerActions(Customer cust,other parameters)

{

CustomerActionsHelper();

}

Public static bool CustomerActions(Customer cust,other parameters,  List<CustomerClicks> = null )

{

CustomerActionsHelper();

}

 

Public static void CustomerActionsHelper()

{

//code to interact with database.

}

Method  3 – CustomerActions method with overloads – a real time one.

 

Conclusion:  Thus above scenario made me and my team to understand the Overlaoding concept of OOP in real time by saving lot of time and repeated work. We had good words for the same work from management. I hope this real time scenario may helped you to understand OOP and it’s one of the feature overloading. Thanks.

Happy Coding 🙂