VB.NET_1.2
VB.NET_1.2
When one sub class inherited from more than one super class is called multiple inheritance.
This type of inheritance contains one sub class and more super classes.
VB.net does not support multiple inheritance directly like java. It can be implemented with the
help of interface.
Interface:-
The body of function and subroutine must be defined within implemented class.
Creating an interface
Syntax:-
Function declaration
End interface
Implementing an interface:-
Syntax:-
Ex-
Module Student
Interface base1
Sub disp()
End Interface
Interface base2
Sub show()
End Interface
Class derived
End Sub
End Sub
End Class
Sub Main()
obj.disp()
obj.show()
End Sub
End Module
Abstract class:-
Polymorphism: The word Polymorphism means one name having many forms.
As phone
As camera
As mp3 player
As radio
There are two types of polymorphism:
compile time polymorphism
runtime polymorphism
Overloading:
Method Overloading means defining multiple methods with the same name
but different parameters.
By using Method Overloading, we can perform different tasks with the
same method name by passing Different parameter.
In visual basic the method overloading is also called compile time Polymorphism or early
binding.
Module overloading
Class geometry
Function area(ByVal r As Double) As Double
Return 3.14 * r * r
End Function
Function area(ByVal a As Integer, ByVal b As Integer) As Integer
Return a * b
End Function
End Class
Sub main()
Dim obj As geometry = New geometry()
Console.WriteLine("Area of a circle" & obj.area(2.5))
Console.WriteLine("Area of a rectangle" & obj.area(2, 5))
Console.ReadKey()
End Sub
End Module
Ex-2
End Sub
Sub sum(ByVal a As Integer, ByVal b As Integer, ByVal c As Integer)
End Sub
Overriding:
Method overriding means override a base class method in the Derived class by
creating a method with the same name and signatures to Perform a different
task.
The method overriding is also called as run time polymorphism or late binding.
Ex-1
Console.writeLine(“Base Class”)
End Sub
End Class
Inherits users
Console.writeLine(“Derived Class”)
End Sub
End Class
Ex-2
End Sub
End Class
End Sub
End Class
End Sub
End Class
Module Module1
Sub Main()
End Sub
End Module
Overloading vs overriding
Destructor:
Syntax:
Protected overrides sub Finalize()
Block of code
End sub
Module destructor
Class test
Private x As Integer
Sub New()
x = 10
End Sub
Sub New(ByVal a As Integer)
x=a
End Sub
Function disp() As Integer
Return x
End Function
Protected Overrides Sub finalize()
Console.WriteLine("The end")
Console.ReadKey()
End Sub
End Class
Sub main()
Dim obj As test = New test()
Dim obj1 As test = New test(20)
Console.WriteLine(obj.disp())
Console.WriteLine(obj1.disp())
Console.ReadKey()
End Sub
End Module
Constructor
Example:
Sub New()
Console.writeline(“Default constructor”)
End sub
ii) Parameterized constructor:-
This type of constructor contains parameter list.
Syntax:-
Sub New(parameter list)
Block of statements
End sub
Example:
Sub New(a as integer, b as integer)
Writeline(“Parameterized constructor”)
End sub
iii) Copy constructor:-
This type of constructor is used to copy value of one object
into another object. It can have one parameter as object of
class.
Syntax:-
Sub New(object of class)
Block of statements
End sub
Example
Sub New(emp as Employee)
Writeline(“Copy constructor”)
End sub
iv)
Module Module1
Class cons
Private l, b As Integer
Sub New()
l=1
b=1
End Sub
Sub New(ByVal x As Integer, ByVal y As Integer)
l=x
b=y
End Sub
Sub New(ByVal ob As cons)
l = ob.l
b = ob.b
End Sub
Function area() As Integer
Return l * b
End Function
End Class
Sub main()
Dim obj1 As cons = New cons()
Dim obj2 As cons = New cons(5, 6)
Dim obj3 As cons = New cons(obj2)
Inheritance:
And it is useful to inherit the properties from one class (base) to another (child) class.
The inheritance will enable us to create a new class by inheriting the properties from
Other classes to reuse, extend and modify the behavior of other class members based on our
requirements.
In visual basic inheritance, the class whose members are inherited is called a base(parent)
class and the class that inherits the members of base(parent) class is called a derived(child)
class.
Advantages:
End Class
Ex-
Class Student
End Class
Class graduation
Inherits student
End class
Ex-
Module Module1
Class laptop
Private ram as integer
Sub input_laptop
Console.writeLine
Base Class
Derived Class
Hierarchical Inheritance
Hierarchical inheritance is a type of inheritance where one class serves as a base class
for multiple derived classes.
Single Inheritance
Module SingleInheritance
Class Rectangle 'super class
Protected l, b, area, peri As Integer
Overridable Sub getInput()
Console.WriteLine("Enter length and breadth") : l = CInt(Console.ReadLine()) : b =
CInt(Console.ReadLine())
End Sub
Overridable Function findArea() As Integer
area = l * b
Return area
End Function
Sub findPeri()
peri = 2 * (l + b)
End Sub
End Class
Class square : Inherits Rectangle
Dim s As Integer
Shadows area, peri As Integer
Overrides Sub getInput()
MyBase.getInput()
Console.WriteLine("Enter side of square")
s = CInt(Console.ReadLine())
End Sub
Public Overrides Function findArea() As Integer
MyBase.findArea()
area = s * s
Return area
End Function
Public Sub calculatePeri()
peri = 4 * s
End Sub
Public Sub getResult()
Console.WriteLine("Area of square=" & area & vbNewLine & "Perimeter of square=" &
peri)
Console.WriteLine("Area of rectangle=" & MyBase.area & vbNewLine & "Perimeter of
rectangle=" & MyBase.peri)
End Sub
End Class
Sub main()
Dim obj As New square
obj.getInput()
obj.findArea()
obj.findPeri()
obj.calculatePeri()
obj.getResult()
Console.ReadKey()
End Sub
End Module
Property(Get/Set)
In visual basic Property is an extension of the class variable And it provides a mechanism
to read, write or change the value
Of the class variable without effecting the external way of Accessing it in our
applications.
blocks called accessors and those are called Get accessor and Set accessor.
By using Get and Set accessors we can change the internal implementation of class
variable and expose it without effecting the external way of accessing it based on our
requirements.
Syntax:
<access_modifier>Property<property_name> As <return_type>
Get
Set
End Set
End Property
Here, the Get accessor code block will be executed whenever the property is read and
the code block of Set accessor will be executed whenever the property is initiated to a
new value.
Ex-
Class Cimage
Get
Return name
End Get
Set(ByVal value As String)
Name=value
End Set
End Property
End Class
E.g:
Get
Return name
End Get
Name=value
End Set
End property
ii) ReadOnly: A property which contains only Get accessor with ReadOnly property, we
E.g:
Get
Return name
End Get
End Property
iii) WriteOnly: A property which contains only set accessor with WriteOnly property,
Name=value
End set
End property
Module Module1
Class Cimage
Get
Return location
End Get
Location=value
End Set
End Property
Get
Return name.ToUpper()
End Get
Name=value
End Set
End Property
Sub Main()
u.name=”Niraj”
u.location=”Patna”
Console.ReadLine()
End Sub
End Module