2. 2
What is Object Oriented Programming?
Object Oriented Programming (OOPs) is based on a real world interpretation of
programming elements.
It is a type of programming in which programmers define not only the data elements but
also the operations that can be performed on the data elements.
An object Is an entity consisting of data as well as actions. For example, consider a car.
It has various features that can describe it. These features could be the company name,
model name, price, mileage and so on. The movements of car such as acceleration,
slowing down could be considered as action.
Object oriented programming uses class and object concept. It has several features and
advantages as well.
3. 3
Features of Object Oriented Programming:
Abstraction: The purpose of abstraction is to separate behaviour from implementation.
It involves extracting essential details of an entity or group of entities while ignoring the
unessential details. Thus, given an entity, the process of separating only the relevant
details from irrelevant details is called abstraction.
Encapsulation: This concept is closely related to abstraction. Encapsulation is the
process of packaging information in such a manner that relevant information is visible
and other details of the object is hidden. In other words it is bundling the relevant details
of an object into a single unit.
Data Hiding: Selective data or processes can be hidden from public view using this
feature. This is necessary to protect certain vital data in the application. Abstraction
identifies what information should be visible and what information can be made
inaccessible to users. Data hiding is the process of making information inaccessible.
Polymorphism: It is the ability of an entity to have many forms. In OOP, this used to
refer to the capability of methods having many forms.
4. 4
What is Class and object?
Class:
A class is a group of different data members or objects with the same properties, processes,
events of an object, and general relationships to other member functions. Furthermore, we can
say that it is like a template or architect that tells what data and function will appear when it is
included in a class object. For example, it represents the method and variable that will work on
the object of the class.
When you define a class, you define a blueprint for a data type. This doesn't actually define
any data, but it does define what the class name means, that is, what an object of the class will
consist of and what operations can be performed on such an object.
Objects:
Objects are instances of a class. The methods and variables that constitute a class are called
members of the class.
Objects are the basic run-time units of a class. Once a class is defined, we can create any
number of objects related to the class to access the defined properties and methods. For
example, the Car is the Class name, and the speed, mileage, and wheels are attributes of the
Class that can be accessed by the Object.
5. 5
Syntax for declaring Class
A class definition starts with the keyword Class followed by the class name; and the class
body, ended by the End Class statement.
Following is the general form of a class definition −
[ Access_Specifier ] [ Shadows ] [ MustInherit | NotInheritable ] [ Partial ] Class ClassName
' Data Members or Variable Declaration
' Methods Name
' Statement to be executed
End Class
Where,
Access_Specifier: It defines the access levels of the class, such as Public, Private or Friend, Protected,
Protected Friend, etc. to use the method. (It is an optional parameter).
Shadows: It is an optional parameter. It represents the re-declaration of variables and hides an identical
element name or set of overloaded elements in a base class.
MustInherit: It is an optional parameter that specifies that the class can only be used as a base class, and
the object will not directly access the base class or the abstract class.
NotInheritable: It is also an optional parameter that representing the class not being used as a base class.
Partial: As the name defines, a Partial represents the partial definition of the class (optional).
Implements: It is used to specify interfaces from which the class inherits (optional).
6. 6
Syntax for creating objects
In the above syntax, we have created an instance (Obj_Name) for the class Class_Name. By
using the object name 'Obj_Name' to access all the data members and the method name
of Class_Name.
Dim Obj_Name As Class_Name = New Class_Name() ' Declaration of object
Obj_Name.Method_Name() ' Access a method using the object
7. 7
Properties and Methods
In Vb.NET data members that describe an object are called fields and properties.
The members which describe object behaviour are called methods.
Properties:
They are used in classes to denote read-only values, Values that once set cannot be changed
and values that can be read as well as changed.
They may also be used to expose the value of private data of an object manner.
They are the extension of variables.
They do not denote storage locations but instead have accessors which specify statements
that can be executed to assign or retrieve values to or from the property.
A set accessor in a property declaration is used to assign values and a Get accessor is used
to retrieve values of the property
8. 8
Classification of Properties
WriteOnly Properties
WriteOnly Properties must have a Set accessor and the Get accessor may be omitted. These
types of properties may be used when the value of the property is likely to change.
ReadOnly Properties
ReadOnly properties must have a Get accessor, however the Set accessor should be omitted.
ReadWrite Properties
A property that does not include either of the keywords ReadOnly or WriteOnly is said to be a
ReadWrite property. This type of property must have a Get accessor as well as a Set acessor.
9. 9
Methods
Methods govern the behaviour of the object.
Methods in a class typically consists of action statements similar to a function or a procedure
10. 10
Constructor
A constructor is a method having the same name as the class within which it is defined. This
method executes automatically every time an object is created.
Constructors are typically declared Public and are used for initialization purposes.
In VB.NET, the lifetime of an object begins when it is created with the New Keyword.
Constructors in VB.NET are written using the Sub New()…End Sub block.
Operation such as opening a files or creating and opening database connections are placed
inside a constructors.
11. 11
Destructor
The lifetime of an object ends when it is destroyed or goes out of scope.
In VB.NET, destructor are written using the Finalize method.
Destructors are normally used for cleanup operations such as releasing of freeing of memory
that is no longer required.
It is called by a system whenever an object is being destroyed.
Syntax:
Overrides Protected Sub Finalize()
…
…
End Sub
12. 12
Example of Constructor
Module Module1
Public Class person
Public name As String
Public gender As Char
Public address As String
Private age As Integer
Public Property agevalue() As String
Set(value As String)
age = value
End Set
Get
Return age
End Get
End Property
Public Sub New()
Console.WriteLine("This is a constructor")
End Sub
Sub main()
Dim student As New person()
'Dim employee As New person
student.name = "Abc"
student.gender = "M"
student.address = "yz"
student.agevalue = "18"
Console.WriteLine("Name of student :" +
student.name)
Console.WriteLine("Gender of student :" +
student.gender)
Console.WriteLine("Address of student :" +
student.address)
Console.WriteLine("Age of student :" +
student.agevalue)
Console.ReadKey()
End Sub
End Module