0% found this document useful (0 votes)
17 views59 pages

UNIT 3 OOP in

Uploaded by

sijisag802
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views59 pages

UNIT 3 OOP in

Uploaded by

sijisag802
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

Object Oriented Programming in

VB.NET

1
2
 Definition
A Method contains series of statements that are executed when
called.

VB. Net defines methods in 2 ways:


1. Sub procedure()
2. Functions()

3
1. Sub procedure( )
Definition:
Procedures are made up of series of statements that when called
are executed.
When application start execution ,control is transferred to main
sub-procedure automatically which is called by default.
By default sub-procedure is public

Each time when sub procedure is called the statement within it


are executed until the END sub is found.
Sub procedures does not return a value.

4
1. Sub procedure( )
Sub procedures does not return a value.

Syntax :
[Modifier] sub procedure_name (parameter_List)
Statements
End sub
Where ,
 Modifiers :specifies the access level(public , private , protected)

 There are 2 methods of parameterized sub procedure

1.Parameter By Value:
Keyword used : ByVal
Pass copy of variable to procedure.
2. Parameter By Reference:
Keyword used : ByRef
Does not Pass copy of variable to procedure.

5
Module Module1
Sub Main()
Console. WriteLine ("In Main()..")
display()
Console. WriteLine ("Return to Main().....")
Console. ReadLine ()
End Sub

Sub display()
Console. WriteLine ("In Display( ) procedure...")
End Sub
End Module

6
Module Module1
Sub Main()
Dim ename As String
Dim eno As Integer
Dim sal As Integer
Console. WriteLine("IN Main().....")
ename = "OM SAI"
eno = 121
sal = 10000
display(ename , eno , sal)
Console. WriteLine("Return in MAin()......")
Console .ReadLine()
End Sub

Sub display(ByVal empname As String, ByVal empno As Integer, sal As Integer)


Console. WriteLine("Employeee Name:" & empname)
Console. WriteLine("Employee No:" & empno)
Console. WriteLine("Employee Salary:" & sal)
End Sub
End Module

7
8
2. Function ( )
Function returns a value.
Used to perform some task in repetitive manner
Task can be evaluate data & to make calculations
Function declaration is same as procedure.
Syntax

[Modifier] Function Function_name (parameter_List) As Return type


Statements
End Function

Where
Modifiers specifies access level(public ,private , protected)
Return type : specifies data type of value to be return.

9
Module Module1
Sub Main()
Console. WriteLine("In Main ()")
display()
Console. WriteLine("Return to Main().....")
Console. ReadLine()
End Sub

Public Function display()


Console. WriteLine("in Display Function...")
End Function

End Module

10
Module Module1
Sub Main()
Dim a, b, c As Integer
Console. WriteLine("Enter value for a:")
a = Console. ReadLine()
Console. WriteLine("Enter value for b:")
b = Console. ReadLine()
c = addition(a, b)
Console. WriteLine("Addition is:" & c)
Console. ReadLine()
End Sub

Public Function addition(ByVal x As Integer, ByVal y As Integer) As Integer


Dim z As Integer
z=x+y
Return z
End Function
End Module

11
12
13
 Object
 Object is the individual instance of (copy of)class.
 Object is the basic runtime entity.
 When program is executed then object interact with each
other by sending messages.
 Syntax

1. Dim obj as New Class_Name


or
2. Dim obj as Class_Name
Obj=New Class_Name
i.e.
Dim obj as Class_Name= New Class_Name()

14
 Class
 Class is collection of similar types of objects.
 Its User defined Data Type.
 Once class is defined ,any no. of objects can be created
which belong that class.
 To use any class we need an object.
 Syntax
Access_Specifier Class Class_Name
Variables, Methods,
Statements
End Class
 Access Specifier: public , private , protected
Syntax : To access the methods of class

Object_Name.Method_Name()

15
Module Module1
Public Class Test
Sub display()
Console.WriteLine("WELCOME OOP IN VB.NET.......")
End Sub
End Class
Sub Main()
Dim obj As New Test
obj. display()
Console. ReadLine ()
End Sub
End Module

16
17
 Constructors
 Is special member function whose task is to initialize the
object of its class.
 This is first method which runs when an instance of class is
created.i.e.it invoked(call) when object is created.
 To create constructor use sub procedure New to a class
 There are 2 types of constructors
1.Default Constructor
2.Parameterized Constructor
1.Default Constructor
 Syntax
Sub New()
Statements
End Sub

18
2.Parameterized Constructor
 Syntax
Sub New( ByVal as Variable_Name As Data_Type)
Statements
End Sub

 Constructor is always declared using Sub keyword and named


New

19
Module Module1
Public Class A
Public Function show()
Console. WriteLine ("This is Base Class..... ")
End Function
Sub New()
Console. WriteLine ("Constructor Executed.......")
End Sub
End Class
Sub Main()
Dim obj As New A
obj. show()
Console. ReadLine()
End Sub
End Module

20
Module Module1
Public Class student
Dim name As String
Public Function display()
Console. WriteLine("I am in Display Method...")
End Function
Sub New( ByVal i As String)
Console. WriteLine("Student Name is:" & i)
End Sub
End Class
Sub Main()
Dim obj As New student("OMSAI")
obj. display()
Console. ReadLine()
End Sub
End Module

21
 Destructors
 Also known as finalizer
 It is the last method run by a class.
 You can place code to clean up the object after its use.
 Finalize () method called automatically , when object is no
longer required.
 Overrides Keyword is used with finalize ()method.
 Syntax:
protected overrides sub finalize()
Statements
End sub

22
Module Module1
Public Class stud
Public Function display()
Console. WriteLine("This is Base Class.....")
End Function
Protected Overrides Sub finalize()
Console. WriteLine("Destructor is Executing.......")
Console. Read()
End Sub
End Class
Sub Main()
Dim obj As New stud
obj. display()
End Sub
End Module

23
24
 Reusability is one of the important feature of OOP.
 Which is achieved by sub-classing a class to extend its
functionality is called inheritance(hierarchical relationship)
 Here, Original class is called the base , parent or super class.
 While the which inherits the functionality of base class & extends
it in own way is called sub ,child or derived class.
 ‘inherits’ keyword is used for inheritance.
 Ex.
Class A Base Class

Class B Derived/Sub Class

 Class ‘B’ inherits class ‘A’ ,means ‘B’ can copy all methods ,
properties of class ‘A’ &B can access all the members of class ‘A’.

25
 Ex. Class A Base Class

Class B Derived/Sub Class

 Syntax
Public class A
Statements
End class
Public class B inherits A
Statements
End class

26
27
 Types of Inheritance
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance

28
1. Single Inheritance
 When single derived class is created from a single Base class
then it is single inheritance
 Ex.
student Base Class

Details Derived/Sub Class

29
Module Module1
Public Class student
Public name As String = "OMSAI"
Public address As String = "PUNE“
End Class

Public Class details


Inherits student

Public Function show()


Console. WriteLine ("STUDENT NAME:" & name)
Console. WriteLine ("STUDENT ADDRESS:" & address)
End Function
End Class

Sub Main()
Dim obj As New details
obj. show()
Console. ReadLine ()
End Sub
End Module
30
31
2. Multi-Level Inheritance
 When a derived class is created from another derived class
then it is Multi-Level inheritance
 Ex.
student Base Class

Branch Derived/Sub Class

Year Derived/Sub Class

32
Module Module1
Public Class student
Public name As String = "OMSAI"
End Class
Public Class branch
Inherits student
Public dept As String = "COMPUTER"
End Class
Public Class year
Inherits branch
Public year As String = "SECOND YEAR"
Public Function show()
Console. WriteLine ("STUDENT NAME:" & name)
Console. WriteLine( "STUDENT DEPARTMENT:" & dept)
Console. WriteLine("STUDENT YEAR:" & year)
End Function
End Class
Sub Main()
Dim obj As New year
obj. show()
Console. ReadLine()
End Sub End Module 33
34
3. Multiple Inheritance
 When a derived class is created from more than one Base
class then it is Multiple inheritance
 VB.NET does not supports Multiple inheritance but it can be
done using interfaces.
 Keyword used :interface
 syntax
Interface identifier
Interface body’ Method name’
End interface

35
Module Module1
Interface Area
Sub area( ByVal a As Integer, ByVal b As Integer)
End Interface
Public Class perimeter
Public Sub per( ByVal p As Integer, ByVal q As Integer)
Dim perimeter As Integer
perimeter = 2 * (p + q)
Console. WriteLine("Perimeter of REctangle is:" & perimeter)
End Sub
End Class
Public Class rectangle
Inherits perimeter
Implements Area
Public Sub area(ByVal l As Integer, ByVal b As Integer) Implements Area. Area
Dim area As Integer
area = l * b
Sub Main()
Console. WriteLine("Area of REctangle is:" & area)
Dim obj As New rectangle
End Sub Console. WriteLine(".......................")
End Class obj. area(10, 10)
Console. WriteLine(".......................")
obj. per(5, 5)
Console. WriteLine(".......................")
Console. ReadLine()
End Sub
36
End Module
37
4. Hierarchical Inheritance
 When more than one derived class is created from single
Base class then it is Hierarchical inheritance
 Ex.

Vehicle Base Class

Bike Car Derived Class

38
Module Module1
Public Class vehicle
Public no_wheel As Integer = 0
End Class
Public Class car
Inherits vehicle
Public Function show()
no_wheel = 4
Console. WriteLine("No of wheels of car:" & no_wheel)
Console. WriteLine("********************")
End Function
End Class
Public Class Bike
Inherits vehicle
Public Function display()
no_wheel = 2
Console. WriteLine("No of wheels of Bike:" & no_wheel)
Console. WriteLine("********************")
Sub Main()
End Function
Dim c As New car()
End Class c. show()
Dim b As New Bike()
b. display()
Console. ReadLine()
End Sub
End Module
39
40
5. Hybrid Inheritance
 If there is any combination of single , hierarchical and Multi-
level inheritance then it is Hybrid Inheritance.
 Ex.
College Base Class

Branch Derived Class of College

SY TY Derived Classes of Branch

41
Module Module1
Public Class college
Public cname As String = "PRAVARA POLYTECHNIC COLLEGE“
End Class
Public Class branch
Inherits college
Public b As String = "COMPUTER“
End Class
Public Class sy
Inherits branch
Public Function show()
Console. WriteLine("SECOND YEAR:" & b)
Console. WriteLine("COLLEGE NAME:" & cname)
End Function
End Class
Public Class ty Sub Main()
Dim s As New sy()
Inherits branch Dim t As New ty
Public Function display() Console. WriteLine(".............................")
Console. WriteLine("THIRD YEAR:" & b) s. show()
Console. WriteLine("COLLEGE NAME:" & cname) Console. WriteLine(".............................")
t. display()
End Function
Console. WriteLine(".............................")
End Class Console. ReadLine()

End Sub
End Module 42
43
44
Overloading
 Means use of same thing for different purpose..

 Using polymorphism ,we can create as many functions as we


want with one function name but with different argument list.

45
Module Module1
Public Class demo
Public i, j As Integer
Public Function add( ByVal i As Integer) As Integer
Return i
End Function
Public Function add( ByVal i As Integer, ByVal j As Integer) As Integer
Return i + j
End Function
End Class

Sub Main()
Dim obj As New demo()
Console. WriteLine("Return i:" & obj. add(10))
Console. WriteLine("Return i+j:" & obj. add(20, 20))
Console. ReadLine()
End Sub
End Module

46
47
48
1. Overriding
 By default ,derived class inherits methods from base class.

 If any inherited property , method need to change in the


derived class then it can be overriden.
 i. e. you can define new implementation of method in derived
class.
 Keyword : overridable used to mark a function as overridable
in base class & overrides in derived class

49
Module Module1
Public Class human
Public Overridable Sub show()
Console. WriteLine("Speaking:")
End Sub
End Class
Public Class indian
Inherits human
Public Overrides Sub show()
Console. WriteLine("Hindi")
End Sub
End Class
Sub Main()
Dim obj1 As New indian
Dim obj2 As New human
obj2.show()
obj1.show()
Console. ReadLine()
End Sub

End Module 50
2. Shadowing
 When two programming elements shares same name ,one of
them can hide/shadows the other one.
 Keyword used : shadows

51
Module Module1
Public Class parent
Public Sub display()
Console. WriteLine("This is Parent class")
End Sub
Public Sub use()
display()
End Sub
End Class

Public Class child


Inherits parent Sub Main()
Public Shadows Sub display() Dim p As New parent
Console. WriteLine("This is child class") Dim c As New child
p. use()
End Sub c. use()
End Class Console. WriteLine("-----------------")
p. display()
c. display()
Console. ReadLine()
End Sub

End Module
52
53
Exception
 Exceptions are the runtime errors.
 Exceptions are unexpected runtime errors in the code which
terminate/end the program.
 VB.NET handles errors and abnormal conditions with exceptions.

Exception Handling
 Is method used to correct or debug the exception.
 This method prevent from unexpected errors.
 Keywords used to handle exceptions are:
1. Try
2. Catch
3. Finally
4. Throw

54
Exception Handling
1. Try
 Includes the statements that might throw exception.
2. Catch
 Handles the exception ,if exists.
3. Finally
 Used for doing any clean-up process.
4. Throw
 Throws exception when problem shows up using ‘Throw’
keyword.

55
Exception Handling Syntax

Try
Statements
Catch ex As Exception
Statements
[Finally]
End Try

 If any exception occurs inside the try block the control transfer to the
appropriate catch block and then control goes to finally block
 Exception can be derived from system .IO .IOException

56
Different Exception Types are:
1.System.IO.IOException
2.System.DivideByZeroException
3.System.StackOverflowException
4.System.IndexOutofRangeException
5.System.OutofMemoryException

 Tostring():Method change the result to string format

57
Module Module1
Sub Main()
Dim result, n1, n2 As Integer
n1 = 10
n2 = 0
Try
result = n1 \ n2
Catch ex As DivideByZeroException
Console. WriteLine("Exception:" & ex.ToString())
Finally
Console. WriteLine("Result is:" & result)

End Try
Console. ReadLine()
End Sub
End Module

58
Module Module1
Sub Main()
Dim age As Integer = 20
Try
If age > 18 Then
Throw New ApplicationException("YOU CAN VOTE....")
Else
Throw New ApplicationException("YOU CAN NOT VOTE....")
End If
Catch ex As Exception
Console. WriteLine(ex. Message)
Finally
Console. WriteLine("THANK YOU.....")
End Try
Console. ReadLine()
End Sub
End Module

59

You might also like