0% found this document useful (0 votes)
16 views18 pages

VB CLASSES AND OBJECTS

Guidlines on making you a genius in codding

Uploaded by

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

VB CLASSES AND OBJECTS

Guidlines on making you a genius in codding

Uploaded by

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

VB.

NET Classes and Object


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.

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. Fo
example, the Car is the Class name, and the speed, mileage, and wheels are attributes o
the Class that can be accessed by the Object.

Creating the Class

We can create a class using the Class keyword, followed by the class name. And the body
of the class ended with the statement End Class. Following is the general syntax fo
creating classes and objects in the VB.NET programming language.

1. [ Access_Specifier ] [ Shadows ] [ MustInherit | NotInheritable ] [ Partial ] Class ClassName


2. ' Data Members or Variable Declaration
3. ' Methods Name
4. ' Statement to be executed
5. End Class

Where,

o 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 optiona
parameter).
o 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.
o 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.
o NotInheritable: It is also an optional parameter that representing the class no
being used as a base class.
o Partial: As the name defines, a Partial represents the partial definition of the class
(optional).
o Implements: It is used to specify interfaces from which the class inherits
(optional).

My_program.vb

1. Public Class My_program


2. ' properties, method name, etc
3. ' Statement to be executed
4. End Class

In the above syntax, we have created a class with the name 'My_program' using the Class
keyword.

The Syntax for creating an object

1. Dim Obj_Name As Class_Name = New Class_Name() ' Declaration of object


2. Obj_Name.Method_Name() ' Access a method using the object

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.

Let's create a program to find the Area and Parameter of a rectangle using the class and
object in VB.NET.

My_program.vb

1. Imports System
2. Module My_program
3. Sub Main()
4. Dim rect As Rectangle = New Rectangle() 'create an object
5. Dim rect2 As Rectangle = New Rectangle() 'create an object
6. Dim area, para As Integer
7.
8. 'rect specification
9. rect.setLength = (5)
10. rect.setBreadth= (6)
11.
12. 'rect2 specification
13. rect2.setLength = (5)
14. rect2.setBreadth = (10)
15.
16. 'Area of rectangle
17. area = rect.length * rect.Breadth
18. 'area = rect.GetArea()
19. Console.WriteLine(" Area of Rectangle is {0}", area)
20.
21. 'Parameter of rectangle
22. 'para = rect.GetParameter()
23. para = 2 (rect2.length + rect.Breadth)
24. Console.WriteLine(" Parameter of Rectangle is {0}", para)
25. Console.WriteLine(" Press any key to exit...")
26. Console.ReadKey()
27. End Sub
28. Public Class Rectangle
29. Public length As Integer
30. Public Breadth As Integer
31.
32. Public Sub setLength(ByVal len As Integer)
33. length = len
34. End Sub
35.
36. Public Sub setBreadth(ByVal bre As Integer)
37. Breadth = bre
38. End Sub
39. Public Function GetArea() As Integer
40. Return length * Breadth
41. End Function
42.
43. Public Function GetParameter() As Integer
44. Return 2 * (length + Breadth)
45. End Function
46. End Class
47. End Module

Output:

Member Function
The member function of a class is used to define the structure of member inside the
definition of the class. It can be accessed by all defined objects of the class and operated
on the data member. Furthermore, member variables are the attributes of an object to be
implemented to a member function. And we can access member variables using the
public member function.

Constructor and Destructor


In VB.NET, the constructor is a special method that is implemented when an object of a
particular class is created. Constructor is also useful for creating and setting defaul
values for a new object of a data member.

When we create a class without defining a constructor, the compiler automatically creates
a default constructor. In this way, there is a constructor that is always available in every
class. Furthermore, we can create more than one constructor in a class with the use
of New keyword but with the different parameters, and it does not return anything.

Default Constructor: In VB.NET, when we create a constructor without defining an


argument, it is called a default constructor.

VB.NET Default Constructor Syntax

The following is the syntax for creating a constructor using the New keyword in VB.NET.

1. Public Class MyClass


2. ' Creates a Constructor using the New
3. Public Sub New()
4. 'Statement to be executed
5. End Sub
6. End Class

Let's create a program to define the default constructor in a VB.NET programming


language.

Default_Const.vb

1. Imports System
2. Module Default_Const
3. Class Tutor
4. Public name As String
5. Public topic As String
6. ' Create a default constructor
7. Public Sub New()
8. name = "JavaTpoint"
9. topic = "VB.NET Tutorial"
10. End Sub
11. End Class
12. Sub Main()
13. ' The constructor will automatically call when the instance of the class is creat
ed
14. Dim tutor As Tutor = New Tutor() ' Create an object as a tutor
15. Console.WriteLine(" Your Site Name is : {0}", tutor.name)
16. Console.WriteLine(" Your Topic Name is : {0}", tutor.topic)
17. Console.WriteLine(" Press any key to exit...")
18. Console.ReadKey()
19. End Sub
20. End Module

Output:
In the above example, we created a class 'Tutor' and defined a default constructo
method with New() keyword without passing any arguments. When the object (tutor) is
created, the default constructor is called into the class.

Parameterized Constructor

In VB.NET, when we pass one or more arguments to a constructor, the constructor is


known as a parameterized constructor. And the object of the class should be initialized
with arguments when it is created.

Let's create a program to use the parameterized constructor to pass the argument in a
class.

Para_Const.vb

1. Imports System
2. Module Para_Const
3. Class Tutor
4. Public name As String
5. Public topic As String
6. ' Create a parameterized constructor to pass parameter
7. Public Sub New(ByVal a As String, ByVal b As String)
8. name = a
9. topic = b
10. Console.WriteLine(" We are using a parameterized constructor to pass the p
arameter")
11. End Sub
12. End Class
13. Sub Main()
14. ' The constructor will automatically call when the instance of the class is creat
ed
15. Dim tutor As Tutor = New Tutor("JavaTpoint", "VB.NET Constructor")
16. Console.WriteLine(" Your Site Name is : {0}", tutor.name)
17. Console.WriteLine(" Your Topic Name is : {0}", tutor.topic)
18. Console.WriteLine(" Press any key to exit...")
19. Console.ReadKey()
20. End Sub
21. End Module

Output:

VB.NET Destructor
In VB.NET, Destructor is a special function that is used to destroy a class object when the
object of the class goes out of scope. It can be represented as the Finalize() method and
does not accept any parameter nor return any value. Furthermore, it can be called
automatically when a class object is not needed.

VB.NET Destructor Syntax

Destructor using the Finalize() method in VB.NET.

1. Class My_Destructor
2. ' define the destructor
3. Protected Overrides Sub Finalize()
4. ' Statement or code to be executed
5. End Sub
6. End Class

Let's create a program to use the Finalize() method in VB.NET Destructor.

Destruct.vb

1. Imports System
2. Module Destruct
3. Class Get_Destroy
4. Public Sub New()
5. Console.WriteLine(" An Object of the class is being created")
6. End Sub
7. ' Use Finalize() method of Destructor
8. Protected Overrides Sub Finalize()
9. Console.WriteLine(" An Object of the class is being destroyed")
10. Console.WriteLine(" Press any key to exit")
11. End Sub
12. End Class
13.
14. Sub Main()
15. Get_Details() ' After invoking the Get_Details() method, garbage collector is cal
ed automatically
16. GC.Collect()
17. Console.ReadKey()
18. End Sub
19. Public Sub Get_Details()
20. Dim dest As Get_Destroy = New Get_Destroy()
21. End Sub
22. End Module

Output:

Constructor Overloading
In VB.NET, the overloading of constructors is a concept in which we can overload
constructors by defining more than one constructor with the same name but with differen
parameter lists to perform different tasks.

Let's create a program to use the Constructor Overloading in a class.

Const_Over.vb

1. Imports System
2. Module Const_Over
3. Class Details
4. Public name As String
5. Public course As String
6. ' Define Default Constructor
7. Public Sub New()
8. name = "Prince"
9. course = "Coputer Science"
10. Console.WriteLine(" Uses of Overloading Constructor")
11. End Sub
12. ' Create a parametrized constructor
13. Public Sub New(ByVal a As String, ByVal b As String)
14. name = a
15. course = b
16. End Sub
17. End Class
18. Sub Main()
19. ' Called default constructor
20. Dim det As Details = New Details()
21. ' Called the parametrized constructor
22. Dim det1 As Details = New Details("Peter", "Knowledge of Data Mining")
23. Console.WriteLine(" Your Details are: Name : {0} and Course : {1}", det.name
det.course)
24. Console.WriteLine(" Your Overloaded Details are: Name : {0} and Course :
{1}", det1.name, det1.course)
25. Console.WriteLine(" Press any key to exit...")
26. Console.ReadKey()
27. End Sub
28. End Module

Output:
Inheritance
In VB.NET, Inheritance is the process of creating a new class by inheriting properties and
functions from the old class and extending the functionality of the existing class. It also
provides a reusable and faster implementation time of the code. When we create a
derived or inherited class, inheritance allows us to inherit all the properties of the existing
class. The old class is known as the base class, and the inherited class is known a
the derived class.

Inheritance Syntax

The following is the syntax of Inheritance in VB.NET.

1. <access_modifier> Class <Name_of_baseClass>


2. ' Implementation of base class
3. End Class
4. <access_modifier> Class <Name_of_derivedClass>
5. Inherits Name_of_baseClass
6. ' Implementation of derived class
7. End Class

Let's create a program to understand the concept of Inheritance in VB.NET

Simple_Inherit.vb

1. Imports System
2. Module Simple_Inherit
3. Public Class Vehicle
4. Public Sub speed()
5. Console.WriteLine(" 4 Wheeler cars are more luxurious than 2 Wheeler")
6. End Sub
7.
8. Public Overridable Sub perform()
9. Console.WriteLine(" Both Vehicles are good, but in a narrow lane, two-wheeler are
more comfortable than 4-Wheeler")
10. End Sub
11. End Class
12.
13. Class Bike
14. Inherits Vehicle
15. Public Overrides Sub perform()
16. Console.WriteLine(" Two-wheeler are lesser weight as compared to 4 wheele
r")
17. End Sub
18. End Class
19. Class Car
20. Inherits Vehicle
21. Public Overrides Sub perform()
22. Console.WriteLine(" It is 4 wheelar car")
23. End Sub
24. End Class
25.
26. Sub Main()
27. Dim vehicle As Vehicle = New Vehicle()
28. Dim bike As Bike = New Bike()
29. Dim car As Car = New Car()
30. vehicle = bike
31. vehicle.perform()
32. vehicle = car
33. vehicle.perform()
34. Console.WriteLine(" Press any ley to exit...")
35. Console.ReadKey()
36. End Sub
37. End Module

Output:
Let's create a program of inheritance using the MyBase in VB.NET.

Inherit_class.vb

1. Module Inherit_class
2. Class Circle 'base class
3. Protected radius As Integer
4. Public Const PI As Double = 3.14
5. Public Sub New(ByVal r As Integer)
6. radius = r
7. End Sub
8. Public Function GetAreaCircle() As Double
9. Return (PI * radius * radius)
10. End Function
11. Public Overridable Sub Show()
12. Console.WriteLine(" Radius of circle : {0}", radius)
13. Console.WriteLine(" Area of circle is {0}", GetAreaCircle())
14. End Sub
15. End Class
16.
17. 'Derived Class
18. Class MyDeriveClass : Inherits Circle
19. Private dimen As Double
20. Public Sub New(ByVal r As Integer)
21. MyBase.New(r)
22. End Sub
23. Public Function Getdimen() As Double
24. Dim dimen As Double
25. dimen = GetArea() * 50
26. Return dimen
27. End Function
28. Public Overrides Sub Show()
29. MyBase.Show()
30. Console.WriteLine("Total Cost: {0}", Getdimen())
31. End Sub
32. End Class
33.
34. Class Circle_Class
35. Shared Sub Main()
36. Dim c As MyDeriveClass = New MyDeriveClass(5)
37. c.Show()
38. Console.WriteLine("Press any key to exit")
39. Console.ReadKey()
40. End Sub
41. End Class
42. End Module

Output:

Multi-Level Inheritance
VB.NET only supports single inheritance that means a class can be inherited from the
base class. However, VB.NET uses hierarchical inheritance to extend one class to anothe
class, which is called Multi-Level Inheritance. For example, Class C extends Class B
and Class B extends Class A, Class C will inherit the member of both class B and Class A
The process of extending one class to another is known as multilevel inheritance.

1. Public Class A
2. ' implementation of code
3. End Class
4.
5. Public Class B
6. Inherits A
7. ' Implementation of Code
8. End Class
9.
10. Public Class C
11. Inherits A
12. ' Implementation of code
13. End Class

Let's create a program to understand the concept of Multi-Level Inheritance in VB.NET

Multi_inherit1.vb

1. Module Multi_inherit1
2. Public Class A
3. Public SName As String
4. Public Sub Display()
5. Console.WriteLine(" Name of Student : {0}", SName)
6. End Sub
7. End Class
8. Public Class B
9. Inherits A
10. Public place As String
11. Public Sub GetPlace()
12. Console.WriteLine(" Address : {0}", place)
13. End Sub
14. End Class
15.
16. Public Class C
17. Inherits B
18. Public rollno As Integer
19. Public Sub GetRollno()
20. Console.WriteLine(" Student Roll no. {0}", rollno)
21. End Sub
22. End Class
23.
24. Class Profile
25. Public Sub Main(ByVal args As String())
26. Dim c As C = New C()
27. c.SName = "Donald Trump"
28. c.place = "USA"
29. c.rollno = 102
30. c.Display()
31. c.GetPlace()
32. c.GetRollno()
33. Console.WriteLine(" Press any key to exit")
34. Console.ReadKey()
35. End Sub
36. End Class
37. End Module

Output:

Interface
In VB.NET, the interface is similar to a class for inheriting all properties, methods, and
events that a class or structure can implement. Using the interface in VB.NET, we can use
multiple inheritances of classes. It uses the Implements keyword to implement the
interfaces, and the Interface keyword is used to create the interface. All interfaces in
VB.NET starts with i.

The Syntax for implementing an interface in a class.

1. Class MyClass
2. Inherits IClass
3. Private Sub FetchDetails() Implements IClass.FetchDetails
4. ' Method Implementation
5. End Sub
6. End Class
In the above snippets, we inherited an interface (IClass) in the Class MyClass tha
implemented to the interface method defined in a class.

Let's create and implement an instance using a class in VB.NET.

Get_Interface.vb

1. Module Get_Interface
2. Interface IStudent
3. Sub Details(ByVal y As String)
4. End Interface
5. Class Student
6. Implements IStudent
7. Public Sub Details(ByVal a As String) Implements IStudent.Details
8. ' Throw New NotImplementedException()
9. Console.WriteLine(" Name of Student: {0}", a)
10. End Sub
11. End Class
12.
13. Class Student1
14. Implements IStudent
15. Public Sub Details(ByVal a As String) Implements IStudent.Details
16. 'Throw New NotImplementedException()
17. Console.WriteLine(" Course: {0}", a)
18. End Sub
19. End Class
20. Sub Main(ByVal args As String())
21. Dim stud As IStudent = New Student()
22. stud.Details(" James Watt")
23. Dim stud1 As IStudent = New Student1()
24. stud1.Details("Computer Science")
25. Console.WriteLine(" Press any key to exit...")
26. Console.ReadKey()
27. End Sub
28. End Module
Output:
Javatpoint Services

You might also like