Chapter No.3 It_ravibaba GAD
Chapter No.3 It_ravibaba GAD
OBJECT ORIENTED
PROGRAMMING IN VB.NET
Introduction
• VB .Net is an Object-Oriented Programming (OOP) language which supports
various OOP concepts such as Class, Objects, Methods, Procedures, Functions,
Inheritance, Constructors and so on.
• VB .Net includes Objects, uniformly, simple variables are also created by classes
and objects, for these purposes it is significant to recognize Object-Oriented
Programming (OOP) in Visual Basic.
• Visual Basic comes with various built-in classes and we recognize that that form
design is also an object.
• Windows forms are objects which derived from the System.Windows.Forms.Form
class and code is the portion of that class.
Classes
• A class is simply an abstract model used to define new data types. It is a template or a
blueprint for an object.
• This blueprint defines attributes for storing data and defines operations for manipulating that
data. Without object we cannot use class.
• In Visual Basic class begins with the keyword Class followed by the Class Name, Class Body
and Ended by the Class Statement.
Syntax:
Example:
[access specifiers] Class ClassName Public Class Student
--------- Variables Private Value As Integer `field
Public Sun getData (ByVal newValue As Integers)
--------- Methods
Value = newValue
--------- Properties
End Sub
--------- Events End Class
End Class
Objects
• An Object is a real-world entity, combine the data and member function in a single unit. An instance
or occurrence of a class is called an object. By using a New keyword, we create an object as follow,
Dim t1 As New Student ()
Yow also can do like this,
Dim t1 As Student = New Student ()
Or
Dimt1 as Student
T1= new Student ()
• Next Significant thing is that, the difference between reference and object. An object is created in
memory using the ‘New’ Keyword and is reference by an identifier called a “reference”.
Example:
Dim mystudent As New Student ()
Or
Dim mystudent As Student
mystudent = new Student ()
Fields
• Fields of a class are like variable and they can be read or set directly.
• Fields are the data contained in the class.
• It is implicit datatypes, objects of some other class.
Example:
Class student
Dim rollno As Integer
Dim name As String
End Class
Here, rollno and name are fields of class.
Properties
• A Property is a special type of method which is considered to assign and retrieve class data. If
the class data is private then we can use property of that class.
• Properties are nothing but wrapper methods on member variables. Each property should be
encompassed at least one GET or SET methods.
• GET method is invoked when you read property and SET method is invoked when you assign
a value to the property.
Syntax:
<access modifier> Property <name of property> As <datatype>
Get
`Some Optional Statements
Return <some private field>
End Get
Set (ByVal <identifier> As <datatype>)
`Some Optional Statements
<some private filed> = <identifier>
End Set
End Property
Methods
Methods are operations that can be performed on data and performance of a class is defined by
methods of that class. A method may take some input values over its parameters and can return a
value of a specific datatype. Methods represent the objects built in procedures.
Example:
Public Class Student
Public Sub Getting ()
MsgBox (“Getting”)
End Sub
Public Function Putting ( ) as string
Return (“Putting”);
End Class
Now, create a new object of the Student Class and call the Getting and Putting methods,
Dim Stud as New Student
Stud. Getting ()
Dim s= stud. Putting ()
Console. writeLine (s)
Constructor
• A constructor is a special type of Sub procedure called when an object is created.
• A constructor method is called before an object of its related class is created. If a class have a
constructor, then the object of that class will be initialized automatically.
• A constructor is like as instance method, but it distinct from a method since it not ever has an
explicit return-type, and can be overridden to provide custom initialization functionality.
• They have the job of initializing the object's data member0s. If you combine a constructor that
initializes an object to a valid state and property methods that only allow valid states, your
objects should remain in a valid state.
Properties of Constructor:
Syntax: 1. It always has the name ‘New’.
Public Class User 2. It does not return any value.
Public Sub New () 3. It is creating the object stated by a class and placed
' Your Custom Code it into a valid state.
End Sub 4. It does not used return type, not even void.
End Class 5. It is automatically called when object of a class is
created.
6. It is always declaring in public section.
1. Default Constructor
A constructor having no parameters or arguments is called as Default Constructor.
Example:
Module Module1
Class Student
Public name, location As String
Public Sub New ()
name = "Alex"
location = "Pune"
End Sub
End Class
Sub Main ()
Dim Student As Student = New Student ()
Console. WriteLine (Student.name)
Console. WriteLine (Student. location)
Console. ReadLine ()
End Sub
End Module
Output:
Alex
Pune
2. Parameterized Constructor
A constructor having parameters or arguments is called as Parameterized Constructor.
Example:
Module Module1
Class Student
Public name, location As String
Public Sub New (ByVal a As String, ByVal b As String)
name = a
location = b
End Sub
End Class
Sub Main ()
Dim Student As Student = New Student ("Alex", "Pune")
Console. WriteLine (Student.name)
Console. WriteLine (Student. Location)
Console. ReadLine ()
End Sub
End Module
Output:
Alex
Pune
Overloaded Constructor (Multiple Constructor)
A constructor having more than one constructor is called as Overloaded Constructor or Multiple Constructor.
Example:
Module Module1
Class Student
Public name, location As String
Public Sub New () // Default Constructor
name = "Alex"
location = "Pune"
End Sub
Public Sub New (ByVal a As String, ByVal b As String) // Parameterized Constructor
name = a
location = b
End Sub
End Class
Sub Main ()
Dim Student As Student = New Student () // Default Constructor is called
// Parameterized Constructor is called
Dim Student1 As Student = New Student ("John", "Mumbai")
Console. WriteLine (Student.name & ", " & Student. location)
Console. ReadLine ()
End Sub
End Module
Destructors
• Destructors in VB .NET are executed as a protected method called as Finalize (). The garbage
collector will call Finalize () method if implemented, but cannot called this method directly.
• Finalize () method cannot overloaded by but should be overridden by using Overrides keyword
in statement. Finalize () method called automatically when the object is no longer required.
• The main effort of the destructor is release resources and inform other objects that the current
objects are going to be destroyed.
Example:
Syntax: Module Module1
Protected Overrides Sub Finalize () Public Class Student
----------- Protected Overrides Sub Finalize ()
Console. WriteLine ("In Finalize Method")
------------
Console. ReadLine ()
End Sub End Sub
End Class
Sub Main ()
Dim obj As New Student ()
End Sub
End Module
Method Overloading
Overloading is the collection of more than one procedure, instance constructor or property in a class with the
same name but different arguments. In VB.Net Overloading can be done by two way:
1. Create two or more methods that have different argument lists within the class.
Syntax:
Public Function Student (ByVal FirstName As String)
……………
End Function
Public Function Student (ByVal LastName As Long)
……………
End Function Syntax for use of Overloads:
Use the “Overloads” keyword. Public Overloads Function Student (ByVal FirstName
As String)
The overloading Overloads keyword is optional.
……………
But if any overloaded member uses the
End Function
Overloads keyword, then all other overloaded
Public Overloads Function Student (ByVal LastName As
members with the same name should also
Long)
specify this keyword.
……………
End Function
Inheritance
• Inheritance is a fundamental feature of an Object-Oriented programming. It is the procedure of
generating a new Class, called the Derived Class, from the existing class, called Base Class.
• Inheritance is a very sophisticated way to reuse and modify the data and functionality that has
previously been defined in the Base Class, also you can add new data and functionality to the
Derived Class.
• Since the Derived Class inherits all properties of the Base Class, the Derived Class has a larger
set of properties than the Base Class. However, the Derived Class may override some or all the
properties of the Base Class. The reuse of existing classes saves time and effort.
Types of Inheritance:
Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Multiple Inheritance
Access Modifiers
• Access Specifiers defines as the scope of accessibility of an Object and its members.
• We can switch the scope of the member object of a class using access specifiers.
• We are using access specifiers for provided that the security of our applications.
• A derived class inherits the properties and methods defined in its base class. This is
beneficial because you can reuse these items when suitable for the derived class.