Overloading and Overriding of the Class

Unknown | 1:49 AM |

Overloading provides the ability to create multiple methods or properties with the same
name, but with different parameters lists. This is a feature of polymorphism. It is
accomplished by using the Overloads keyword in VB.NET. A simple example would be
an addition function, which will add the numbers if two integer parameters are passed to
it and concatenate the strings if two strings are passed to it.


Class test
Public Overloads Function Add(ByVal x As Integer, ByVal y As
Integer)
Return x + y
End Function
Public Overloads Function Add(ByVal x As String, ByVal y As
String)
Return x & y
End Function
Shared Sub main()
Dim a As new test
Dim b As Integer
Dim c As String
b = a.Add(1, 2)
c = a.Add("Reshmi", " Nair")
System.Console.Writeline(b)
System.Console.Writeline(c)
End Sub
End Class
O/P:
3
Reshmi Nair



Overriding
Class inheritance causes the methods and properties present in the base class also to be
derived into the derived class. There might arise a situation wherein you would like to
change the functionality of an inherited method or property. In such cases we can
override the method or property of the base class. This is another feature of
polymorphism. You can accomplish this in VB.NET by using the Overridable keyword
with the base class method and the Overrides keyword with the derived class method.
Public Class shapes
Public Overridable Sub display()
Console.WriteLine("Shapes")
End Sub
End Class
Public Class square
Inherits shapes
Public Overrides Sub display()
Console.WriteLine("This is a square")
End Sub
End Class
Public Class rectangle
Inherits shapes
Public Overrides Sub display()
Console.WriteLine("This is a rectangle")
End Sub
End Class
The above example is just an indication to how overriding can be implemented in either
VB.NET.

Category:

About http://dotnetvisual.blogspot.in/:
DOT NET TO ASP.NET is a web application framework marketed by Microsoft that programmers can use to build dynamic web sites, web applications and web services. It is part of Microsoft's .NET platform and is the successor to Microsoft's Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime, allowing programmers to write ASP.NET code using any Microsoft .NET language. create an application very easily ....