1
A Programme Under the Compumitra Series
Programming Primer –
Polymorphism (Method Overloading)
LAB WORK GUIDE
2
OUTLINE
Polymorphism
 Method Overloading (using
VB)
Example Template Creation.
Code Creation.
Output Evaluation.
Example Explanation.
Error Trials.
Home Exercise.
Review Summary.
3
Polymorphism
(Method Overloading)
Using VB
Method overloading is a one of the techniques of creating multiple polymorphic
classes with variation in its number and type of parameters. For e.g.
MyMethod(param1) and MyMethod(param1, param2) are two overloaded methods
of same name but they act differently on the basis of parameters.
4
OverloadingVB– Creating Button and Label Template
Button with 'Text' Property set
to 'Method Overloading'.
Three Labels to hold the place
for output display with 'text'
property set to NULL (blank).
• Follow Standard Website Creation Steps and set your path to
C:Learner<student-id>ProgrammingPrimerOverloadingVB
• Now Create the Execution Event Handler as a button and output
place holders as follows.
5
OverloadingVB – Copy/Paste Code-1
Dim mc1 As New My_Class
Label1.Text = mc1.MyMethod()
Label2.Text = mc1.MyMethod("abc", 10)
Label3.Text = mc1.MyMethod("xyz", "lmn")
Go to 'Default.aspx.vb' by double clicking
on 'Button' ('Method Overloading' button)
of 'Default.aspx' and 'Paste' the Code in
'Button1_Click' method
Dim mc1 As New My_Class
Label1.Text = mc1.MyMethod()
Label2.Text = mc1.MyMethod("abc", 10)
Label3.Text = mc1.MyMethod("xyz", "lmn")
Copy this Code
6
OverloadingVB - Copy Code-2
Class My_Class
Public Overloads Function MyMethod() As String
Return("Output from MyMethod, which takes no parameter")
End Function
Public Overloads Function MyMethod(ByVal a As String, ByVal b As Integer) As
String
Return("Output from MyMethod, which takes two parameters, one as String
and second as Integer")
End Function
Public Overloads Function MyMethod(ByVal s As String, ByVal c As String) As
String
Return("Output from MyMethod, which takes two String type parameters")
End Function
End Class 'My_Class
Copy this Code
7
OverloadingVB - Paste Code-2
Class My_Class
Public Overloads Function MyMethod() As String
Return("Output from MyMethod, which takes no parameter")
End Function
Public Overloads Function MyMethod(ByVal a As String, ByVal b As Integer) As String
Return("Output from MyMethod, which takes two parameters, one as String and second as
Integer")
End Function
Public Overloads Function MyMethod(ByVal s As String, ByVal c As String) As String
Return("Output from MyMethod, which takes two String type parameters")
End Function
End Class 'My_Class
Run Code By
pressing 'F5'
Paste code after the
'End' of '_Default' class
Dim mc1 As New My_Class
Label1.Text = mc1.MyMethod()
Label2.Text = mc1.MyMethod("abc", 10)
Label3.Text = mc1.MyMethod("xyz", "lmn")
8
OverloadingVB - Output
Output on browser after pressing the button.
9
Partial Class _Default Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim mc1 As New My_Class
Label1.Text = mc1.MyMethod()
Label2.Text = mc1.MyMethod("abc", 10)
Label3.Text = mc1.MyMethod("xyz", "lmn")
End Sub
End Class
Class My_Class
Public Overloads Function MyMethod() As String
Return ("Output from MyMethod, which takes no parameter")
End Function
Public Overloads Function MyMethod(ByVal a As String, ByVal b As Integer) As String
Return ("Output from MyMethod, which takes two parameters, one as String
and second as Integer")
End Function
Public Overloads Function MyMethod(ByVal s As String, ByVal c As String) As String
Return ("Output from MyMethod, which takes two String types parameters")
End Function
End Class 'My_Class
OverloadingVB – Example Explanation - 1
This is 'My_Class' class, which has
multiple 'MyMethod' methods.
This statement creates the object 'mc1' of
'My_Class' class.
10
Partial Class _Default Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim mc1 As New My_Class
Label1.Text = mc1.MyMethod()
Label2.Text = mc1.MyMethod("abc", 10)
Label3.Text = mc1.MyMethod("xyz", "lmn")
End Sub
End Class
Class My_Class
Public Overloads Function MyMethod() As String
Return ("Output from MyMethod, which takes no parameter")
End Function
Public Overloads Function MyMethod(ByVal a As String, ByVal b As Integer) As String
Return ("Output from MyMethod, which takes two parameters, one as String
and second as Integer")
End Function
Public Overloads Function MyMethod(ByVal s As String, ByVal c As String) As String
Return ("Output from MyMethod, which takes two String types parameters")
End Function
End Class 'My_Class
OverloadingVB – Example Explanation - 2
This statement call the method
'MyMethod()' of 'My_Class' without
passing any 'parameter' to it
When we call the method 'MyMethod'
without passing any parameter
Then this method will invoke.
Similarly other 'MyMethod'
Functions shall be invoked
based on type of parameters.
Watch the use of 'Overloads' keyword in each function.
11
OverloadingVB : Error Trials
 Try removing 'Overloads' keyword from any one method. Watch the runtime error as follows:
BC31409: function 'MyMethod' must be declared 'Overloads' because another 'MyMethod' is declared 'Overloads' or
'Overrides'.
You may also try to move the mouse over the function MyMethod (Shown with sawtooth underline) in your code editing
window. See the tooltip as follows to watch the expected error in advance as follows.
You can see that your IDE helps you to find problems in advance.
 Try defining another method in My_Class as follows and watch the runtime error.
Public Overloads Function MyMethod(ByVal str1 As String, ByVal str2 As String) As
String
Return ("Output from trial method")
End Function
BC30269: 'Public Overloads Function MyMethod(s As String, c As String) As String' has multiple definitions with identical
signatures.
You can now notice that only one function with a particular type of parameter is possible even though the parameter
names defined are different.
12
OverloadingVB : Home Exercise
 You are required to make a program where you can
declare a class 'Shape'.
 Make a public method area where the radius parameter is
passed. Within method write the formula 3.14 * r * r for area
of circle.
 Make another public method area where two sides value
parameters are passed. Within method write formula side1 *
side2 for area of a rectangle.
 Now write some event handler to show functionality of getting
the area of circle or area of a rectangle using the same method
area( ).
We have told you that overloading is on the basis of similarly named functions
but with different set of parameters.
You can try creating similar exercises for yourself to master the variations of
parameter types. Is overloading required if a method of same name is defined in
another class.
Oh! Its all too confusing, its better to go back to my other hobbies.
13
Method OverLoadingVB : Learning Summary Review
 Concept of Method Overloading
 Method overloading is one of the fundamental type of polymorphic behavior in
OOPS.
 In this case multiple methods or functions can be defined with same name but with
different number and type of parameters.
 Example of area of circle or rectangle using same method area( ):
Class Shape
Overloaded Function area(radius)
return ( 3.14 * radius * radius)
End Function
Overloaded Function area(side1,side2)
return ( side1 * side2)
End Function
End Class
(above is a demonstration code only)
 Method Overloading is not required across classes as here same naming is
already permitted due Class by itself having the encapsulation property.
14
Ask and guide me at
sunmitraeducation@gmail.com
Share this information with as
many people as possible.
Keep visiting www.sunmitra.com
for programme updates.

Progamming Primer Polymorphism (Method Overloading) VB

  • 1.
    1 A Programme Underthe Compumitra Series Programming Primer – Polymorphism (Method Overloading) LAB WORK GUIDE
  • 2.
    2 OUTLINE Polymorphism  Method Overloading(using VB) Example Template Creation. Code Creation. Output Evaluation. Example Explanation. Error Trials. Home Exercise. Review Summary.
  • 3.
    3 Polymorphism (Method Overloading) Using VB Methodoverloading is a one of the techniques of creating multiple polymorphic classes with variation in its number and type of parameters. For e.g. MyMethod(param1) and MyMethod(param1, param2) are two overloaded methods of same name but they act differently on the basis of parameters.
  • 4.
    4 OverloadingVB– Creating Buttonand Label Template Button with 'Text' Property set to 'Method Overloading'. Three Labels to hold the place for output display with 'text' property set to NULL (blank). • Follow Standard Website Creation Steps and set your path to C:Learner<student-id>ProgrammingPrimerOverloadingVB • Now Create the Execution Event Handler as a button and output place holders as follows.
  • 5.
    5 OverloadingVB – Copy/PasteCode-1 Dim mc1 As New My_Class Label1.Text = mc1.MyMethod() Label2.Text = mc1.MyMethod("abc", 10) Label3.Text = mc1.MyMethod("xyz", "lmn") Go to 'Default.aspx.vb' by double clicking on 'Button' ('Method Overloading' button) of 'Default.aspx' and 'Paste' the Code in 'Button1_Click' method Dim mc1 As New My_Class Label1.Text = mc1.MyMethod() Label2.Text = mc1.MyMethod("abc", 10) Label3.Text = mc1.MyMethod("xyz", "lmn") Copy this Code
  • 6.
    6 OverloadingVB - CopyCode-2 Class My_Class Public Overloads Function MyMethod() As String Return("Output from MyMethod, which takes no parameter") End Function Public Overloads Function MyMethod(ByVal a As String, ByVal b As Integer) As String Return("Output from MyMethod, which takes two parameters, one as String and second as Integer") End Function Public Overloads Function MyMethod(ByVal s As String, ByVal c As String) As String Return("Output from MyMethod, which takes two String type parameters") End Function End Class 'My_Class Copy this Code
  • 7.
    7 OverloadingVB - PasteCode-2 Class My_Class Public Overloads Function MyMethod() As String Return("Output from MyMethod, which takes no parameter") End Function Public Overloads Function MyMethod(ByVal a As String, ByVal b As Integer) As String Return("Output from MyMethod, which takes two parameters, one as String and second as Integer") End Function Public Overloads Function MyMethod(ByVal s As String, ByVal c As String) As String Return("Output from MyMethod, which takes two String type parameters") End Function End Class 'My_Class Run Code By pressing 'F5' Paste code after the 'End' of '_Default' class Dim mc1 As New My_Class Label1.Text = mc1.MyMethod() Label2.Text = mc1.MyMethod("abc", 10) Label3.Text = mc1.MyMethod("xyz", "lmn")
  • 8.
    8 OverloadingVB - Output Outputon browser after pressing the button.
  • 9.
    9 Partial Class _DefaultInherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim mc1 As New My_Class Label1.Text = mc1.MyMethod() Label2.Text = mc1.MyMethod("abc", 10) Label3.Text = mc1.MyMethod("xyz", "lmn") End Sub End Class Class My_Class Public Overloads Function MyMethod() As String Return ("Output from MyMethod, which takes no parameter") End Function Public Overloads Function MyMethod(ByVal a As String, ByVal b As Integer) As String Return ("Output from MyMethod, which takes two parameters, one as String and second as Integer") End Function Public Overloads Function MyMethod(ByVal s As String, ByVal c As String) As String Return ("Output from MyMethod, which takes two String types parameters") End Function End Class 'My_Class OverloadingVB – Example Explanation - 1 This is 'My_Class' class, which has multiple 'MyMethod' methods. This statement creates the object 'mc1' of 'My_Class' class.
  • 10.
    10 Partial Class _DefaultInherits System.Web.UI.Page Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim mc1 As New My_Class Label1.Text = mc1.MyMethod() Label2.Text = mc1.MyMethod("abc", 10) Label3.Text = mc1.MyMethod("xyz", "lmn") End Sub End Class Class My_Class Public Overloads Function MyMethod() As String Return ("Output from MyMethod, which takes no parameter") End Function Public Overloads Function MyMethod(ByVal a As String, ByVal b As Integer) As String Return ("Output from MyMethod, which takes two parameters, one as String and second as Integer") End Function Public Overloads Function MyMethod(ByVal s As String, ByVal c As String) As String Return ("Output from MyMethod, which takes two String types parameters") End Function End Class 'My_Class OverloadingVB – Example Explanation - 2 This statement call the method 'MyMethod()' of 'My_Class' without passing any 'parameter' to it When we call the method 'MyMethod' without passing any parameter Then this method will invoke. Similarly other 'MyMethod' Functions shall be invoked based on type of parameters. Watch the use of 'Overloads' keyword in each function.
  • 11.
    11 OverloadingVB : ErrorTrials  Try removing 'Overloads' keyword from any one method. Watch the runtime error as follows: BC31409: function 'MyMethod' must be declared 'Overloads' because another 'MyMethod' is declared 'Overloads' or 'Overrides'. You may also try to move the mouse over the function MyMethod (Shown with sawtooth underline) in your code editing window. See the tooltip as follows to watch the expected error in advance as follows. You can see that your IDE helps you to find problems in advance.  Try defining another method in My_Class as follows and watch the runtime error. Public Overloads Function MyMethod(ByVal str1 As String, ByVal str2 As String) As String Return ("Output from trial method") End Function BC30269: 'Public Overloads Function MyMethod(s As String, c As String) As String' has multiple definitions with identical signatures. You can now notice that only one function with a particular type of parameter is possible even though the parameter names defined are different.
  • 12.
    12 OverloadingVB : HomeExercise  You are required to make a program where you can declare a class 'Shape'.  Make a public method area where the radius parameter is passed. Within method write the formula 3.14 * r * r for area of circle.  Make another public method area where two sides value parameters are passed. Within method write formula side1 * side2 for area of a rectangle.  Now write some event handler to show functionality of getting the area of circle or area of a rectangle using the same method area( ). We have told you that overloading is on the basis of similarly named functions but with different set of parameters. You can try creating similar exercises for yourself to master the variations of parameter types. Is overloading required if a method of same name is defined in another class. Oh! Its all too confusing, its better to go back to my other hobbies.
  • 13.
    13 Method OverLoadingVB :Learning Summary Review  Concept of Method Overloading  Method overloading is one of the fundamental type of polymorphic behavior in OOPS.  In this case multiple methods or functions can be defined with same name but with different number and type of parameters.  Example of area of circle or rectangle using same method area( ): Class Shape Overloaded Function area(radius) return ( 3.14 * radius * radius) End Function Overloaded Function area(side1,side2) return ( side1 * side2) End Function End Class (above is a demonstration code only)  Method Overloading is not required across classes as here same naming is already permitted due Class by itself having the encapsulation property.
  • 14.
    14 Ask and guideme at sunmitraeducation@gmail.com Share this information with as many people as possible. Keep visiting www.sunmitra.com for programme updates.