Computer Science(9608) notes and Resources
Polymorphism is an object-oriented programming concept that refers to the ability of a variable, function or object to take on multiple forms. A language that features polymorphism allows developers to program in the general rather than program in the specific. There are two types of polymprphism. They are:
i. Compile time polymorphism
ii. Run time Polymorphism
Compile time polymorphism:- compile time polymorphism achieved by "Method Overloading", means that same name function with different parameters in same class called compile time polymorphism.
Example 1
Module Module1
Public Class One
Public i, j, k As Integer
Public Function add(ByVal i As Integer) As Integer
'function with one argument
Return i
End Function
Public Function add(ByVal i As Integer, ByVal j As Integer) As Integer
'function with two arguments
Return i + j
End Function
Public Function add(ByVal i As Integer, ByVal j As Integer, ByVal k As Integer) As Integer
'function with three arguments
Return i + j + k
End Function
End Class
Sub Main()
Dim objone As New One()
Console.WriteLine(objone.add(10))
'calls the function with one argument
Console.WriteLine(objone.add(10, 20))
'calls the function with two arguments
Console.WriteLine(objone.add(10, 20, 30))
'calls the function with three arguments
Console.ReadLine()
End Sub
End Module
Example 2
Module Module1
Public Class Polymorphism
Public A, B, C As Integer
Public Function Sum(ByVal A As Integer) As Integer
'function with one argument
Return A
End Function
Public Function Sum(ByVal A As Integer, ByVal B As Integer) As Integer
'function with two arguments
Return A + B
End Function
Public Function Sum(ByVal A As Integer, ByVal B As Integer, ByVal C As Integer) As Integer
'function with three arguments
Return A + B + C
End Function
End Class
Sub Main()
Dim Objpolymorphism As New Polymorphism()
Console.WriteLine(Objpolymorphism.Sum(20))
'calls the function with one argument
Console.WriteLine(Objpolymorphism.Sum(20, 40))
'calls the function with two arguments
Console.WriteLine(Objpolymorphism.Sum(20, 40, 60))
'calls the function with three arguments
Console.ReadLine()
End Sub
End Module
Example 3
Module Module1
Class test
Dim r As Double
Public Sub area(ByVal r As Double)
Console.Write("Area of the Circle :")
Console.WriteLine(1 / 3 * 3.14 * r * r * r)
End Sub
Dim length As Integer
Dim width As Integer
Public Sub area(ByVal length As Integer, ByVal width As Integer)
Console.Write(" Area of the Rectangle :")
Console.WriteLine(length * width)
End Sub
End Class
Sub Main()
Dim objtest As New test()
objtest.area(3)
objtest.area(4, 5)
Console.ReadLine()
End Sub
End Module
Run time Polymorphism:- Run time polymorphism achieved by "Method Overriding or Operator Overloading", means that same name function with same parameters in different different classes called Run time Polymorphism.
Overriding : Overriding in VB.net is method by which a inherited property or a method is overidden to perform a different functionality in a derived class. The base class function is declared using a keyword Overridable and the derived class function where the functionality is changed contains an keyword Overrides.
Example 1
Module Module1
Class over
Public Overridable Function add(ByVal x As Integer, ByVal y As Integer)
Console.WriteLine("Function Inside Base Class")
Return (x + y)
End Function
End Class
Class over2 : Inherits over
Public Overrides Function add(ByVal x As Integer, ByVal y As Integer)
Console.WriteLine(MyBase.add(120, 100))
Console.WriteLine("Function Inside Derived Class")
Return (x + y)
End Function
End Class
Sub Main()
Dim objover2 As New over2
Console.WriteLine(objover2.add(10, 100))
Console.ReadLine()
End Sub
End Module
Description: In the above overriding example the base class function add is overridden in the derived class using the MyBase.add(120,100) statement. So first the overidden value is displayed, then the value from the derived class is displayed.
Example 2
Module Module1
Public Class test
Public Overridable Function show()
Console.WriteLine("my country")
End Function
End Class
Public Class test2 : Inherits test
Public Overrides Function show()
Console.WriteLine(MyBase.show())
Console.WriteLine("nepal")
End Function
End Class
Sub Main()
Dim objtest2 As test2 = New test2()
objtest2.show()
Console.ReadLine()
End Sub
End Module
- CIE A-LEVEL COMPUTER SCIENCE ( 9608 )
No comments:
Post a Comment
Comments here....