VB.NET — Study Notes

Unit 04

OOP in .NET — Classes, Properties, Access Specifiers, Keywords & Core Concepts

6 Questions 6 Marks Each VB.NET
01
6 Marks Question

OOP in .NET — Creating Classes, Object Creation and Destruction

Introduction to OOP in .NET

Object Oriented Programming (OOP) in .NET is a programming approach based on classes and objects. It helps in organising programs into reusable and modular units.

Core Concepts
  • A class is a blueprint for creating objects.
  • An object is an instance of a class.
  • OOP improves code reusability, maintainability, and scalability.

Creating Classes

A class is defined using the Class keyword. It contains data members (variables), methods (functions), and properties.

Syntax
[Access Specifier] Class ClassName
    ' Data members
    ' Methods
End Class

Example

VB.NET Code
Public Class MyProgram
    Dim a, b As Integer

    Public Sub Display()
        a = 10
        b = 20
        MsgBox("Result = " & (a + b))
    End Sub
End Class
OutputResult = 30

Object Creation

An object is created using the New keyword. The object is then used to access class members. When an object is created, the constructor is automatically executed.

Syntax
Dim objName As ClassName = New ClassName()

Example

Dim obj As MyProgram = New MyProgram()
obj.Display()
Constructor & Destructor

Constructor (Object Initialization)

A constructor is a special method that is automatically called when an object is created. It is used to initialize the object.

Syntax
Public Sub New()
    ' Initialization code
End Sub

Object Destruction (Destructor)

A destructor is used to destroy an object and release resources when it is no longer needed. In VB.NET it is defined using the Finalize() method and is automatically called by the Garbage Collector.

Syntax
Protected Overrides Sub Finalize()
    ' Cleanup code
End Sub
Conclusion OOP in .NET allows developers to build structured and reusable applications. Classes define the structure, objects provide implementation, constructors initialize objects, and destructors clean up resources.
02
6 Marks Question

Properties, Methods and Events in Classes

Introduction

In OOP, a class contains properties, methods, and events which define the behaviour and characteristics of objects.

Property Properties Store and retrieve data

Properties provide controlled access to data using Get and Set procedures. Get returns the value; Set assigns the value.

Syntax
Private fieldName As DataType

Public Property PropertyName() As DataType
    Get
        Return fieldName
    End Get
    Set(ByVal value As DataType)
        fieldName = value
    End Set
End Property
Example
Public Class Student
    Private name As String

    Public Property Name() As String
        Get
            Return name
        End Get
        Set(ByVal value As String)
            name = value
        End Set
    End Property
End Class
Method Methods Define behaviour

Methods are functions or procedures defined inside a class that perform specific tasks. They define the behaviour of an object and can accept parameters and return values.

Syntax
Public Sub MethodName()
    ' Statements
End Sub
Example
Public Sub Display()
    MsgBox("Hello World")
End Sub
Event Events Handle actions & triggers

Events are used to trigger actions when a specific activity occurs. They allow communication between objects and are commonly used in GUI applications (e.g., button click).

Syntax
Public Event EventName(ByVal msg As String)
Example
Public Class ClickEvent
    Public Event TripleClick(ByVal msg As String)

    Public Sub Click()
        RaiseEvent TripleClick("Button Clicked")
    End Sub
End Class
Conclusion Properties, methods, and events are essential components of a class. Together they make classes dynamic, interactive, and efficient.
  • Properties — manage data
  • Methods — define behaviour
  • Events — handle actions
03
6 Marks Question

Access Specifiers in VB.NET

Introduction

Access specifiers in VB.NET define the accessibility or visibility of class members (variables, methods, properties). They control where and how these members can be accessed in a program.

Public Accessible from anywhere in the program

Members declared as Public can be accessed from:

  • Same class
  • Other classes in the same project
  • Other projects
Example
Public Class Book
    Public title As String
End Class
Private Accessible only within the same class

Members declared as Private cannot be accessed outside the class. They can only be used through public methods or properties.

Example
Public Class Book
    Private title As String

    Public Sub SetTitle(t As String)
        title = t
    End Sub
End Class
Protected Accessible in class and derived classes

Members declared as Protected are accessible within the same class and in derived (child) classes through inheritance. Not accessible outside the class hierarchy.

Example
Public Class Base
    Protected value As Integer
End Class

Public Class Derived
    Inherits Base

    Public Sub Show()
        value = 10   ' Accessible here
    End Sub
End Class
Protected Friend Combination of Protected and Friend

Members are accessible within the same project (assembly) and also in derived classes, even outside the project.

Example
Public Class Example
    Protected Friend data As Integer
End Class
Conclusion Access specifiers provide security and control over class members. They help implement data hiding and encapsulation in OOP.
  • Public → Accessible everywhere
  • Private → Accessible only within the class
  • Protected → Accessible in class and derived classes
  • Protected Friend → Accessible in project and derived classes
04
6 Marks Question

Me, MyBase and MyClass Keywords

Introduction

Me, MyBase, and MyClass are special keywords used to refer to objects and classes within inheritance and OOP. They help in accessing members of the current class and base class.

Me Refers to the current instance of the class Self-reference

Used to access current class members (variables, methods, properties). Useful when there is a naming conflict between parameters and class variables.

  • Refers to the current object
  • Resolves naming conflicts between local and class variables
Example
Public Class Student
    Private name As String

    Public Sub SetName(ByVal name As String)
        Me.name = name   ' Me.name = class variable
    End Sub
End Class
MyBase Refers to the base (parent) class Parent access

Allows access to base class methods, properties, or constructors. Commonly used in inheritance.

  • Calls parent class methods from a derived class
  • Used with Inherits keyword
Example
Public Class Base
    Public Sub Show()
        MsgBox("Base Class Method")
    End Sub
End Class

Public Class Derived
    Inherits Base

    Public Sub Display()
        MyBase.Show()   ' Calls base class method
    End Sub
End Class
MyClass Refers to the current class itself Avoids polymorphism

Calls methods of the current class even if they are overridden in a derived class. It avoids polymorphism (late binding).

  • Refers to the current class, not the instance
  • Ignores method overriding in derived classes
Example
Public Class Base
    Public Overridable Sub Show()
        MsgBox("Base Class")
    End Sub
End Class

Public Class Derived
    Inherits Base

    Public Overrides Sub Show()
        MsgBox("Derived Class")
    End Sub

    Public Sub Test()
        MyClass.Show()   ' Always calls Derived's version
    End Sub
End Class
Conclusion These keywords help in managing inheritance and accessing class members effectively.
  • Me → Refers to current object
  • MyBase → Refers to base (parent) class
  • MyClass → Refers to current class (ignores overriding)
05
6 Marks Question

Encapsulation, Abstraction and Polymorphism

Introduction

Encapsulation, Abstraction, and Polymorphism are fundamental concepts of OOP. They help in designing secure, flexible, and reusable programs.

Encapsulation Binding data and methods into a single unit Protects data

Encapsulation is the process of binding data and methods together into a single unit (class) and restricting direct access to data. Achieved using access specifiers (Private, Public, etc.) with data accessed through properties or methods.

Example
Public Class Student
    Private name As String

    Public Property Name() As String
        Get
            Return name
        End Get
        Set(ByVal value As String)
            name = value
        End Set
    End Property
End Class
Key Point — Provides data hiding and security.
Abstraction Hiding complexity, showing only essentials Reduces complexity

Abstraction is the process of hiding complex implementation details and showing only essential features. Achieved using abstract classes or methods. Focuses on what to do, not how to do.

Example
MustInherit Class Shape
    Public MustOverride Sub Draw()
End Class

Class Circle
    Inherits Shape

    Public Overrides Sub Draw()
        MsgBox("Drawing Circle")
    End Sub
End Class
Key Point — Reduces complexity and improves usability.
Polymorphism One name, many forms Increases flexibility

Polymorphism means one name, many forms. It allows methods to behave differently based on the object. Types: Compile-time (Overloading) and Run-time (Overriding).

Example — Method Overriding (Run-time)
Public Class Animal
    Public Overridable Sub Sound()
        MsgBox("Animal Sound")
    End Sub
End Class

Public Class Dog
    Inherits Animal

    Public Overrides Sub Sound()
        MsgBox("Bark")
    End Sub
End Class
Key Point — Increases flexibility and reusability.
Conclusion These concepts make programs secure, simple, and efficient in OOP.
  • Encapsulation → Protects data
  • Abstraction → Hides complexity
  • Polymorphism → Provides flexibility
06
6 Marks Question

Interface and Inheritance

Introduction

Interface and Inheritance are important OOP concepts in VB.NET that help in achieving code reusability, flexibility, and modular design.

Inheritance Child class acquires properties of parent class Code reuse

Inheritance is the process by which one class (derived/child) acquires the properties and methods of another class (base/parent). Achieved using the Inherits keyword.

Syntax
Class DerivedClass
    Inherits BaseClass
End Class
Example
Public Class Animal
    Public Sub Eat()
        MsgBox("Eating")
    End Sub
End Class

Public Class Dog
    Inherits Animal

    Public Sub Bark()
        MsgBox("Barking")
    End Sub
End Class

Here, Dog can use both Eat() and Bark() methods.

Types of Inheritance

1
Single Inheritance — One base class, one derived class.
2
Multilevel Inheritance — Derived class becomes base for another class.
3
Hierarchical Inheritance — One base class, multiple derived classes.
Interface A contract that a class must follow No implementation

An interface is a collection of method declarations without implementation. It defines a contract that a class must follow. Declared using Interface keyword and implemented using Implements.

Syntax
Interface InterfaceName
    Sub MethodName()
End Interface
Example
Public Interface IShape
    Sub Draw()
End Interface

Public Class Circle
    Implements IShape

    Public Sub Draw() Implements IShape.Draw
        MsgBox("Drawing Circle")
    End Sub
End Class

The class must implement all methods declared in the interface.

Difference Between Interface and Inheritance

BasisInheritanceInterface
DefinitionChild class reuses code from a base classDefines method signatures only, no implementation
KeywordInheritsInterface / Implements
ImplementationMethods have body in base classNo method body — class must implement
MultipleA class can inherit only one classA class can implement multiple interfaces
Conclusion Both concepts improve flexibility, maintainability, and structure of programs in VB.NET.
  • Inheritance → Enables code reuse and hierarchical relationships
  • Interface → Ensures implementation of required methods