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.
- 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.
[Access Specifier] Class ClassName
' Data members
' Methods
End Class
Example
VB.NET CodePublic Class MyProgram
Dim a, b As Integer
Public Sub Display()
a = 10
b = 20
MsgBox("Result = " & (a + b))
End Sub
End Class
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.
Dim objName As ClassName = New ClassName()
Example
Dim obj As MyProgram = New MyProgram()
obj.Display()
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.
SyntaxPublic 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.
Protected Overrides Sub Finalize()
' Cleanup code
End Sub
Properties, Methods and Events in Classes
Introduction
In OOP, a class contains properties, methods, and events which define the behaviour and characteristics of objects.
Properties provide controlled access to data using Get and Set procedures. Get returns the value; Set assigns the value.
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
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.
SyntaxPublic Sub MethodName()
' Statements
End Sub
Example
Public Sub Display()
MsgBox("Hello World")
End Sub
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).
SyntaxPublic 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
Properties— manage dataMethods— define behaviourEvents— handle actions
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.
Members declared as Public can be accessed from:
- Same class
- Other classes in the same project
- Other projects
Public Class Book
Public title As String
End Class
Members declared as Private cannot be accessed outside the class. They can only be used through public methods or properties.
Public Class Book
Private title As String
Public Sub SetTitle(t As String)
title = t
End Sub
End Class
Members declared as Protected are accessible within the same class and in derived (child) classes through inheritance. Not accessible outside the class hierarchy.
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
Members are accessible within the same project (assembly) and also in derived classes, even outside the project.
ExamplePublic Class Example
Protected Friend data As Integer
End Class
Public→ Accessible everywherePrivate→ Accessible only within the classProtected→ Accessible in class and derived classesProtected Friend→ Accessible in project and derived classes
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.
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
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
Allows access to base class methods, properties, or constructors. Commonly used in inheritance.
- Calls parent class methods from a derived class
- Used with
Inheritskeyword
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
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
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
Me→ Refers to current objectMyBase→ Refers to base (parent) classMyClass→ Refers to current class (ignores overriding)
Encapsulation, Abstraction and Polymorphism
Introduction
Encapsulation, Abstraction, and Polymorphism are fundamental concepts of OOP. They help in designing secure, flexible, and reusable programs.
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.
ExamplePublic 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
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.
ExampleMustInherit Class Shape
Public MustOverride Sub Draw()
End Class
Class Circle
Inherits Shape
Public Overrides Sub Draw()
MsgBox("Drawing Circle")
End Sub
End Class
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
Encapsulation→ Protects dataAbstraction→ Hides complexityPolymorphism→ Provides flexibility
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 is the process by which one class (derived/child) acquires the properties and methods of another class (base/parent). Achieved using the Inherits keyword.
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
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.
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
| Basis | Inheritance | Interface |
|---|---|---|
| Definition | Child class reuses code from a base class | Defines method signatures only, no implementation |
| Keyword | Inherits | Interface / Implements |
| Implementation | Methods have body in base class | No method body — class must implement |
| Multiple | A class can inherit only one class | A class can implement multiple interfaces |
Inheritance→ Enables code reuse and hierarchical relationshipsInterface→ Ensures implementation of required methods