Using the Method Overloading and Method Overriding features in C# 13
Method Overloading
In programming, methods within a class might have different argument lists but the same name thanks to a technique called method overloading. This type of compile-time polymorphism determines which method to execute at compile time based on the method signature. Method overloading necessitates that the parameters of the overloaded methods be distinct types, numbers, or both. By enabling methods to accomplish the same operation with varied inputs without needing different method names, this feature improves the readability and maintainability of code.
Definition
A compile-time polymorphism feature called method overloading allows multiple methods in a class with the same name but differing parameter lists.
Key Points
- overloaded Methods must have different parameter types, numbers, or both.
- A return type alone does not distinguish overloaded methods from non-overloaded methods.
Code Example of Method Overloading
The Add function is overloaded with numerous parameter configurations in the code example below, enabling the class to handle a variety of input conditions. The same method name can be used for several purposes, each with a different combination of parameter types or counts, thanks to method overloading. This guarantees that the code remains clear and simple while allowing the Add function to handle a variety of inputs, including adding integers, floating-point values, and even complicated combinations. By using method overloading, the class gains flexibility and resilience. This eliminates the need to identify each variant individually and enables a simplified interface to satisfy a range of operational requirements.
Method Overriding
Object-oriented programming uses method overriding as a way of providing a specific implementation for a method already defined in its base class. Runtime polymorphism uses this feature to enable dynamic method dispatch, which determines the method to be executed based on the type of the object at runtime. During method overriding, the base class method must be marked with the virtual keyword so that it can be overridden in derived classes, ensuring clarity and ensuring that the overriding behaviour is intentional. The override keyword must be explicitly used in the derived class method intended to override the base class method. Additionally, the method signature of the derived class must be the same as the method signature of the base class. By adhering to these rules, method overriding facilitates a more flexible and extensible code structure, allowing subclasses to redefine or enhance inherited methods.
Definition
Using method overriding, a subclass can provide a specific implementation for a method that has already been defined in its base class. This is called runtime polymorphism.
Key Points
- For a method to be virtual, it must be marked with the virtual keyword.
- The override keyword must be used in the derived class method.
- The signature of the method in the derived class must match the signature of the method in the base class.
Code Example of Method Overriding
In this code example below, the Speak method is overridden in the Dog and Cat classes to provide specific behaviors tailored to these subclasses, while adhering to the same interface defined in the base class. By overriding a common method signature defined in a base class, derived classes can implement it differently. Dog and Cat classes can display their unique ways of “speaking” by overriding the Speak method, which does not change the underlying structure of the base class. In object-oriented programming, polymorphism demonstrates its versatility and practicality by ensuring consistency in method usage while allowing for diverse behaviours.