Javatpoint Logo
Javatpoint Logo

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

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 for creating classes and objects in the VB.NET programming language.

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

My_program.vb

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

The Syntax for creating an 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

Output:

VB.NET Classes and Object

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

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

Default_Const.vb

Output:

VB.NET Classes and Object

In the above example, we created a class 'Tutor' and defined a default constructor 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

Output:

VB.NET Classes and Object

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.

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

Destruct.vb

Output:

VB.NET Classes and Object

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 different parameter lists to perform different tasks.

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

Const_Over.vb

Output:

VB.NET Classes and Object

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 as the derived class.

Inheritance Syntax

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

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

Simple_Inherit.vb

Output:

VB.NET Classes and Object

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

Inherit_class.vb

Output:

VB.NET Classes and Object

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

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

Multi_inherit1.vb

Output:

VB.NET Classes and Object

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.

In the above snippets, we inherited an interface (IClass) in the Class MyClass that 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

Output:

VB.NET Classes and Object




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA