0% found this document useful (0 votes)
4 views

VB.NET_1.2

The document explains key concepts of object-oriented programming in VB.net, including multiple inheritance, interfaces, abstract classes, polymorphism, method overloading and overriding, destructors, constructors, and inheritance types. It details how to implement these concepts with syntax examples and highlights the advantages of using inheritance in application development. Additionally, it covers properties in VB.net, including read-write, read-only, and write-only properties.

Uploaded by

Vaibhav Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

VB.NET_1.2

The document explains key concepts of object-oriented programming in VB.net, including multiple inheritance, interfaces, abstract classes, polymorphism, method overloading and overriding, destructors, constructors, and inheritance types. It details how to implement these concepts with syntax examples and highlights the advantages of using inheritance in application development. Additionally, it covers properties in VB.net, including read-write, read-only, and write-only properties.

Uploaded by

Vaibhav Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Multiple inheritance:-

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:-

An interface is mainly used to achieve multiple inheritance.

It also supports data abstraction.

An interface is a collection of only function and subroutine declaration.

It is created with the help of “interface” keyword.

We can not create an object of interface.

It must be implemented within class.

The body of function and subroutine must be defined within implemented class.

The method of interface must be accessed publically.

Creating an interface
Syntax:-

Interface <interface name>

Function declaration

End interface

Implementing an interface:-

Implements:- This keyword is used to implement an interface.

Syntax:-

Class <class name>

Implements <interface name>

Ex-

Module Student

Interface base1

Sub disp()

End Interface

Interface base2

Sub show()
End Interface

Class derived

Implements base1, base2

Public Sub disp() Implements base1.disp

Console.WriteLine("I'm in base 1 class method")

End Sub

Public Sub show() Implements base2.show

Console.WriteLine("I'm in base 2 class method")

End Sub

End Class

Sub Main()

Dim obj As derived = New derived()

obj.disp()

obj.show()
End Sub

End Module

Abstract class:-

 This class is used to achieve data abstraction.


 It is declared with the help of “mustinherit” keyword.
 It can have abstract function & subroutine. Its body must be
defined within sub class. An abstract class can have only
declaration of abstract function.
 An abstract function must be declared with the help of “mustoverrides “ keyword.
 An abstract class must be inherited.
 Its object can not be created.
 It can have also constructor and member function.

Polymorphism: The word Polymorphism means one name having many forms.

One function behaves in different forms. In other words, "Many forms of

a single object is called Polymorphism."


Example :

Your mobile phone, one name but 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.

Notes: Overloads keyword may be used to create method overloading.


Ex-1

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

Public class Calculate

Sub sum(ByVal a As Integer, ByVal b As Integer)

Console.writeLine(“sum of two no” & (a+b))

End Sub
Sub sum(ByVal a As Integer, ByVal b As Integer, ByVal c As Integer)

Console.WriteLine(“Sum of three number” & (a+b+c))

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 can be achieved by using Overridable & overrides


keywords along with the inheritance principle.

 The method overriding is also called as run time polymorphism or late binding.

Suppose, if we want to change the behavior of the base class

Method in a derived class, we need to use method overriding.

The base class method which we want to override in the derived


Class that needs to be defined with an overridable keyword and we need to use overrides
keyword in derived class while defining the method with same name and parameters then only
we can override the base class method in the derived class.

Ex-1

Public class users

Public overridable sub display()

Console.writeLine(“Base Class”)

End Sub

End Class

Public class Details

Inherits users

Public overrides sub display()

Console.writeLine(“Derived Class”)

End Sub
End Class

Ex-2

Public Class Shape

Public Overridable Sub Draw()

Console.WriteLine("Shape ko draw kiya gaya hai.")

End Sub

End Class

Public Class Circle : Inherits Shape

Public Overrides Sub Draw()

Console.WriteLine("Circle ko draw kiya gaya hai.")

End Sub

End Class

Public Class Rectangle : Inherits Shape

Public Overrides Sub Draw()


Console.WriteLine("Rectangle ko draw kiya gaya hai.")

End Sub

End Class

Module Module1

Sub Main()

Dim shape As Shape = New Shape()

shape.Draw() ' Output: Shape ko draw kiya gaya hai.

shape = New Circle()

shape.Draw() ' Output: Circle ko draw kiya gaya hai.

shape = New Rectangle()

shape.Draw() ' Output: Rectangle ko draw kiya gaya hai.

End Sub

End Module
Overloading vs overriding

 Overloading is compile time concept or called early binding.


 Overriding is run time concept or called late binding
 Same name with different signature
 Same name with same signature
 Using overloads keyword
 Using overridable and overrides keywords

Destructor:

 Destructor is a special method of a class and it is useful


in class to destroy the object.
 Destructors can be used only in classes and a class can contain only one Destructor.
 The destructor in class can be represented by using Finalize() method.
 The destructor in visual basic won‟t accept any parameters.
 The destructor will invoke automatically whenever an
Instance of a class is no longer needed.
 The destructor automatically invoked by garbage collector whenever The class objects
that are no longer needed in an application.

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

 In visual basic, constructor is a method and it will invoke automatically


whenever an instance of class is created.
 The constructor is useful to initialize and set default values for the data members of the new
object.
 It does not return any value so it is treated as subroutine instead of function.
i) Default constructor:-
This type of constructor does not contain parameter list.
Syntax:-
Sub New()
Block of statements
End sub

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)

Console.WriteLine("Area=" & obj1.area())


Console.WriteLine("Area=" & obj2.area())
Console.WriteLine("Area=" & obj3.area())
Console.ReadKey()
End Sub
End Module

Inheritance:

In visual basic, inheritance is one of the primary concepts of object-oriented programming


(OOP) and it is useful to inherit the properties from one class
(base) to another(child) class.

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:

Application development time is less

Application takes less memory

Application execution time is less

Application performance is enhanced

Repetition of the code is reduced.


Syntax:

Using “Inherits” keyword

Access_modifier class sub_class_ name: Inherits super_class_name

End Class

Ex-

Class Student

End Class

Class graduation

Inherits student

End class

Ex-

Module Module1

Class laptop
Private ram as integer

Private hdd as integer

Sub input_laptop

Console.writeLine

Single Inheritance (One to One):

If a sub class is inherited from only one base class.

Base Class

Derived Class

Multi level inheritance:-


An extending single level inheritance is called multi-level inheritance.

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.

In visual basic properties can contain one or two code

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.

Generally, in object-oriented programming language like visual basic we need to define


fields as Private and use the properties to access their values in Public way with Get and
Set accessors.

Syntax:

<access_modifier>Property<property_name> As <return_type>

Get

„Return property value


End Get

Set

„set a new value

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

Private name as String

Public Property Uname as String

Get

Return name

End Get
Set(ByVal value As String)

Name=value

End Set

End Property

End Class

Dim k as cimage =New Cimage()

K.Uname=”kumar” „ Set accessor will invoke

Console.writeLine(K.Uname) „ Get accessor will invoke

In visual basic,the properties are categorized as three types:

Read-Write: A property which contains a both Get and Set accessors

With property keyword,we will call it as read-write property.

E.g:

Public property Uname as string

Get
Return name

End Get

Set(ByVal value as string)

Name=value

End Set

End property

ii) ReadOnly: A property which contains only Get accessor with ReadOnly property, we

will call it as read-only property.

E.g:

Public Readonly property uname as string

Get

Return name

End Get

End Property

iii) WriteOnly: A property which contains only set accessor with WriteOnly property,

we will call it as write-only property.


E.g:

Public writeOnly property uname as string

Set(ByVal value as string)

Name=value

End set

End property

Module Module1

Class Cimage

Private location as string

Private name as string

Public Property ulocation As String

Get

Return location
End Get

Set (Byval value as string)

Location=value

End Set

End Property

Public property uname as string

Get

Return name.ToUpper()

End Get

Set(Byval value as string)

Name=value

End Set

End Property

Sub Main()

Dim u as cimage=new cimage()

u.name=”Niraj”
u.location=”Patna”

Console.WriteLine(“Name : “ & u.name)

Console.writeLine(“Location “ & u.location)

Console.WriteLine(“Press Enter key to Exit”)

Console.ReadLine()

End Sub

End Module

You might also like