0% found this document useful (0 votes)
12 views20 pages

Chapter No.3 It_ravibaba GAD

The document provides an overview of Object-Oriented Programming (OOP) concepts in VB.NET, including classes, objects, methods, properties, constructors, and inheritance. It explains the syntax and functionality of these concepts, along with examples for clarity. Additionally, it covers exception handling and polymorphism, emphasizing the importance of access modifiers and structured exception handling in programming.
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)
12 views20 pages

Chapter No.3 It_ravibaba GAD

The document provides an overview of Object-Oriented Programming (OOP) concepts in VB.NET, including classes, objects, methods, properties, constructors, and inheritance. It explains the syntax and functionality of these concepts, along with examples for clarity. Additionally, it covers exception handling and polymorphism, emphasizing the importance of access modifiers and structured exception handling in programming.
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/ 20

CHAPTER – I

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.

Access Modifiers Description

Defines a type that is publicly accessible. For example, a public


Public
method of a class can be accessed from anywhere in the program.
Defines a type that is accessible only from within the context in
which it is declared. For instance, a Private variable declared within
Private a class module is accessible only from within that class module. A
Private class is accessible only from classes within which it is
nested.
Applies to class members only. Defines a type that is accessible
Protected
only from within its own class or from a derived class.
Friend Defines a type that is accessible only from within the program.
Defines a type that is accessible from within the and from derived.
Protected Friend
classes.
Method Overriding
• Method Overloading gives a developer the capability to write a better-quality of the method
of the base class without disturbing the message passed by the programs, which uses
derived class.

• 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.

Overriding Properties and methods in Derived Classes:


1. Overridable: Allows a property or method in a class to be overridden in a derived class.
2. Overrides: Overrides an Overridable property or method defined in the base class
3. NotOverridable: Avoids a property or method from overridden in an inheriting class. By
default, Public Methods are NotOverridable.
4. MustOverride: Needs that a derived class overrides the property or method. When the
MustOverride keyword is used, the definition of method contains the Sub, Function or
Property statement. Other statements are not permitted and precisely there is no End Sub or
End Function statement. MustOverrides methods must be declared in MustInherit classes.
Interfaces
• Interfaces describe the properties, methods, and events that classes can be implemented. Interfaces
permit to express features of closely related properties, methods, and events; decrease difficulties
since develop enhanced implementations for interfaces without exposing existing code.
• Also add new features at any time by developing additional interfaces and implementations.
Sometimes, interface is referred to as ‘Contract’.
• Syntax: Properties of Interfaces:
[access-modifier] Interface identifier 1. A user defined data type is similar to class but covers
[interface-bases] all abstract methods.
Interface-body 2. By default, all methods are abstract and public.
End interface 3. In child class all methods are overridden.
4. Permits to implement the multiple inheritance.
5. A class can inherit only one class but any number of
interfaces.
6. implements keyword to implement the interface.
7. use the Interface keyword to create an interface.
8. An interface can inherit other interfaces.
9. An interface is a type with members that are all public.
Polymorphism
• Polymorphism is the ability to take different forms. It allocates objects of a derived class to
variables of the base class. It is nothing but the one thing in many forms.
There are Two types of Polymorphism:
1. Compile Time Polymorphism
• Compile Time polymorphism accomplished by “Method Overloading”. Compile Time
Polymorphism concept of overloading same function name with different arguments in same class.
• At compile time which function will be invoked will be determined by checking the datatype and
number of arguments in the function. In VB.Net the Overloads keyword is optional when
overloading, but if any overloaded member uses the Overloads keyword, then all other overloaded
members with the same name must also specify this keyword.

2. Run Time Polymorphism:


• Run Time Polymorphism accomplished by “Method Overriding”. Method overriding is using
same function name with same arguments in different classes.
• In VB.Net overriding realized by the keywords overridable in base class methods and overrided
and overrides keywords in the base class method.
Exception Handling
Exceptions are the incidence of some condition that changes the normal flow of
execution. For example, if your program run out of memory, file does not exist in the
given path, network connections are dropped etc., then it is a Runtime Errors.
There are three types of errors in VB.Net,
1. Syntax Error: When we write a code that is not permitted by the rules of language.
For example, incorrect typed keyword or an incorrect syntax.
2. Run Time Error: When a statement attempts an operation that is unbearable to carry
out. For example, referencing an object that is inaccessible. Run Time errors arises
during execution of code.
3. Logical Error: When an application does not perform the way, it was intended to
perform. These errors are difficult to find because a logic error not generate the error
massage.
Structured Exception Handling
• An exception is a problem that occurs during the execution of a program. An exception
is a response to a special condition that arises while a program is running, such as an
attempt to divide by zero.
• Exceptions deliver a method to transfer control from one part of a program to another.
• VB.Net exception handling is built on four keywords-Try, Catch, Finally and Throw.
 Try − A Try block recognizes a block of code for which specific exceptions will be
triggered. It's having one or more Catch blocks.
 Catch − A program catches an exception with an exception handler at the place in a
program where to handle the problem. The Catch keyword specifies the catching of an
exception.
 Finally − The Finally block is used to execute a given set of statements, whether an
exception is thrown or not. For example, if you open a file, it should be closed whether
an exception is raised or not.
 Throw − A program throws an exception when a problem shows up. This is done using a
Throw keyword.

You might also like