Compile time polymorphism - Method OverLoading
Polymorphism is an important concept of OOPs. Polymorphism feature enables classes to provide different implementation of methods having the same name. This feature allows us to define any number of methods with the same name but different signatures. A signature is a method's name and the number, type and the order of parameters.
This is also sometimes called as method overloading. In method overloading, a method is executed depending on the number and type of parameters passed to it.
When we compile the class, the compiler binds the appropriate method to the object based on the method's arguments. This is called early binding and this process is referred to as compile time polymorphism. The example given below demonstrates compile time polymorphism using overloaded methods.
In the above code, we overload the method 'Sum' by passing different types of values and call each of the overloaded Sum methods from the Form_Load method.
This is also sometimes called as method overloading. In method overloading, a method is executed depending on the number and type of parameters passed to it.
When we compile the class, the compiler binds the appropriate method to the object based on the method's arguments. This is called early binding and this process is referred to as compile time polymorphism. The example given below demonstrates compile time polymorphism using overloaded methods.
Public Class ArthemeticSum Public Function Sum(ByVal a As Integer, ByVal b As Integer) As Integer Dim result = a + b Return result End Function Public Function Sum(ByVal a As Long, ByVal b As Long) As Long Dim result = a + b Return result End Function Public Function Sum(ByVal a As Double, ByVal b As Double) As Double Dim result = a + b Return result End Function End Class Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim obj As ArthemeticSum = New ArthemeticSum() Dim intTot = obj.Sum(10, 10) MessageBox.Show(intTot.ToString()) Dim DoubleTot = obj.Sum(10.2, 10.89) MessageBox.Show(DoubleTot.ToString()) End Sub
In the above code, we overload the method 'Sum' by passing different types of values and call each of the overloaded Sum methods from the Form_Load method.
Object Oriented Programming Articles
- Abstract class constructors
- abstract class inheritance in c#.net
- abstract class inheritance in visual basic.net
- New C# Language Feature: Automatic Properties
- Calling base class constructor in C#
- base keyword in c#
- Compile time polymorphism - Method OverLoading
- How to initialize Base class Object from inside Derived Class constructor
- Constructor overloading in vb.net
- How to use inheritance in c#
- How to use inheritance in visual basic.net
- Parameter array in vb.net
- Procedure OverLoading in vb.net
- Visual Inheritance with Windows Forms
- Azure Platform
- GridView CommandField example
- GridView and DetailsView Master/Detail page using SqlDataSource control
- POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
- Query entity data model using linq to entities
- Difference between arraylist and list collection
- How to create a Web service using Visual Studio.net
- FormView DataBound Event
- Calling base class constructor in C#
- Convert a sequence to a generic list using ToList()method
- Project ideas for students
- Accounting Software
- Creating an ASP.Net MVC 3 application
- Using assemblies in .net applications
- How to implement form validation using ASP.Net 2.0 Validation Controls
- Constructors in Visual Basic.Net