Module7: Object-Oriented
Programming (OOP) in Visual
Basic
ICT 211
Introduction to OOP Concepts
Object-Oriented Programming (OOP) is a programming paradigm
based on the concept of "objects," which contain both data and
methods. In Visual Basic, OOP is implemented through classes and
objects, making it easier to build complex, reusable, and maintainable
code.
Key OOP Concepts:
•Classes: Templates for creating objects. They define the
properties (attributes) and methods (behaviors) that objects
created from the class can have.
•Objects: Instances of classes that hold specific data and
functionality defined by their class.
•Inheritance: The ability of a class to inherit properties and
methods from another class, promoting code reusability.
•Polymorphism: The ability to present the same interface for
different underlying data types, allowing objects of different types
to be treated as if they are instances of the same class.
Creating and Using Classes
In Visual Basic, classes are created using the Class keyword. A
class defines the structure and behavior of an object by specifying
properties and methods.
Public Class Car
' Properties
Example: Public Property Make As String
Public Property Model As String
Creating a Public Property Year As Integer
simple Car class.
' Constructor
Public Sub New(make As String, model As
String, year As Integer)
[Link] = make
[Link] = model
[Link] = year
End Sub
' Method
Public Function GetCarInfo() As String
Return "Car: " & Make & " " & Model &
", Year: " & [Link]()
End Function
End Class
To use this Car class, you create an object of the class:
Dim myCar As New Car("Toyota", "Corolla", 2022)
[Link]([Link]())
Explanation:
•Properties: Make, Model, and Year represent attributes of the Car class.
•Constructor: Initializes the object with specific values for Make, Model, and Year.
•Method: GetCarInfo returns a string with details of the car.
Encapsulation and Abstraction
•Encapsulation and abstraction are two important principles in OOP
that help manage complexity in code.
Encapsulation:
Encapsulation: Hides the internal state of an object and only
allows modification through methods, which protects the data
from external manipulation. In Visual Basic, encapsulation is
achieved by setting properties as Public or Private.
Example:
Private _make As String
Public Property Make As String
Get
Return _make
End Get
Set(value As String)
_make = value
End Set
End Property
The _make field is private, and access is controlled through the Make property.
Abstraction
Abstraction: Focuses on exposing only the essential details while
hiding the unnecessary details. By using classes and methods, we
create a simpler interface for users of the class.
Abstraction
•In Object-Oriented Programming, abstraction is the concept of
exposing only essential features of an object while hiding the complex
implementation details. This helps to simplify interaction with objects,
as users interact with the object through a clean, easy-to-understand
interface
Cont
In Visual Basic, abstraction can be achieved by using abstract
classes (using MustInherit) and interfaces. Let’s look at an example of
abstraction with a MustInherit class, which defines a template without
exposing all implementation details.
Example
Suppose we have a scenario where different types of bank accounts
exist, such as SavingsAccount and CheckingAccount. We can create an
abstract base class called BankAccount to define the common methods for all
account types without providing the detailed implementations for each.
' Abstract Class
Public MustInherit Class BankAccount
' Property to store the balance ' Abstract method to withdraw money (no
Protected Balance As Decimal implementation here)
Public MustOverride Sub Withdraw(amount As
' Constructor Decimal)
Public Sub New(initialBalance As Decimal)
Balance = initialBalance ' Concrete method to get the balance
End Sub Public Function GetBalance() As Decimal
Return Balance
' Abstract method to deposit money (no End Function
implementation here) End Class
Public MustOverride Sub Deposit(amount As
Decimal)
Explanation
The BankAccount class is abstract (MustInherit),
meaning you cannot create instances of it directly.
The Deposit and Withdraw methods are marked as
abstract (MustOverride) without implementation
details, forcing subclasses to define their own
implementations.
The GetBalance method is implemented in the
BankAccount class and is available to all
subclasses, so they don’t need to implement it.
Implementing Inheritance and Polymorphism in Visual Basic
Inheritance allows a class (subclass) to inherit properties and
methods from another class (base class), promoting code reusability
and enabling polymorphism.
Example
Let's create a Vehicle base class and then extend
it with a Car subclass.
Example ' Subclass
Public Class Car
' Base class Inherits Vehicle
Public Class Vehicle Public Property Model As String
Public Property Speed As Integer
Public Property FuelType As String Public Sub New(speed As Integer, fuelType
As String, model As String)
Public Sub New(speed As Integer, fuelType [Link](speed, fuelType)
As String) [Link] = model
[Link] = speed End Sub
[Link] = fuelType
End Sub Public Overrides Function GetInfo() As
String
Public Overridable Function GetInfo() As Return [Link]() & ", Model: " &
String Model
Return "Speed: " & Speed & ", Fuel End Function
Type: " & FuelType End Class
End Function
End Class
Explanation:
The Vehicle class is a base class with properties
Speed and FuelType.
Car inherits from Vehicle and adds the Model
property.
[Link] calls the constructor of the base class.
The GetInfo method in Car overrides the base class
GetInfo method using Overrides, providing
additional functionality.
Polymorphism in Visual Basic
Polymorphism allows you to call the same method on different
objects and get different results. It’s achieved through method
overriding, as shown in the inheritance example above.
Example
Dim myVehicle As Vehicle = New Vehicle(60, "Diesel")
Dim myCar As Vehicle = New Car(120, "Petrol", "Honda
Civic")
[Link]([Link]()) ' Output:
Speed: 60, Fuel Type: Diesel
[Link]([Link]()) ' Output:
Speed: 120, Fuel Type: Petrol, Model: Honda Civic
GetInfo is called on both myVehicle and myCar, but because myCar is an instance of Car, it uses the overridden
version of GetInfo.
Summary
•Classes and Objects are fundamental to OOP in Visual
Basic.
•Inheritance allows code reuse, while Polymorphism allows
methods to be used in multiple ways.
•Encapsulation and Abstraction help manage complexity by
hiding implementation details and exposing only what is
necessary.
Class Exercises
•Suppose we have a scenario where different types of
bank accounts exist, such as SavingsAccount and
CheckingAccount. We can create an abstract base class
called BankAccount to define the common methods for all
account types without providing the detailed
implementations for each
[Link](“Thank You for Attending !!
Check Source Code in GitHub")