C# Basics-Part 3(Method Overloading,Method Chaining,Method Overriding)

Methods are used in our code to implement common and specific behavior.
We add a method to our class that defines the operations of the class.

Today we will talk about some concepts around the methods, let's start

Method Overloading

Here we have multiple methods with the same name and purpose however with a different signature.
They also have the same return type.

Requirement

Suppose in your automation project you want to write a common method that writes text to say textbox, text area etc. You have 2 choices here, you can clear the existing text and write a new one or you can append the new text to existing text if any.

We can use method overloading here, we will add  2 SendKeys methods, one takes a single parameter(the text which one wants to write) and other method takes 2 parameters (text and the clearFlag, which can be used to clear/not clear the input before entering the text)

Check below code




































We could have solved the requirement using single Sendkeys method having 2 parameters, where the user can choose to clear or not to clear the input however using method overloading makes it easier for the calling code where he can use multiple flavors of the method as per his needs.

Note - Above implementation of SendKeys is not the best and used only to explain the method overloading concept.


Method Chaining

In our above example, we see that we are writing the code to write to a textbox in both the methods
//Code to write in Textbox goes here
           Console.WriteLine("Successfully Written the text : " + text);

We could refactor our code here 💪
We can make a call from 2nd SendKeys method to 1st SendKeysmethod, check the below code







































Well that's what is Method chaining, one method overload calls another method overload 🙌

Method Overriding

It allows overriding the logic of method that is defined up in an object hierarchy.

All the classes which we defined are inherited from the Object class and we can override virtual methods of the object class.

One such method is the ToString method which returns a string representation of an object.






















Requirement
Now suppose we have a requirement where if one calls the ToString method of the object it has to return the name plus the purpose of the class.

To meet the requirement we can override the ToString method and add the purpose of the class and return the entire string.

Check the below code

































Here we created an instance of DriverManager in the Main method.

Note- We are storing the instance in the driverManager variable whose type is  Object.
As Object is Parent class of DriverManager we can do so.
We will talk about inheritance in detail in coming lectures.

We have added override keyword to the ToString method signature, tells the compiler that you are overriding the logic

We can also give a call to Object class ToString method using below code
base.ToString();

And finally, we just concated the results from the base class and the purposeStr variable

Points to remember

  1. Parent class method should be declared as virtual so that it can be overridden by the child class
  2. override keyword tells the compiler that you are overriding the logic of base class method
  3. base keyword used in a subclass to call parent class method

That brings us to the end of this lecture 👌

Comments

Popular posts from this blog

Specflow -Part3(Working with tables using Specflow.Assist.Dynamic)

Specflow -Part4(Specflow Scenario Outline and Feature Background )

Specflow -Part6(Specflow Hooks)