Object-Oriented Programming
Visual Basic 2015 is an object-oriented programming language. In this lesson, you will learn the basic ideas of OOP, including classes, objects, properties, methods, and inheritance. :contentReference[oaicite:2]{index=2}
25.1 Introduction to OOP
Object-Oriented Programming, or OOP, is a style of programming that organizes code around classes and objects. A class is a blueprint, while an object is an actual instance created from that blueprint. :contentReference[oaicite:3]{index=3}
This approach helps programmers build programs that are easier to understand, reuse, and maintain.
- Class → blueprint
- Object → instance created from a class
- Property → data belonging to an object
- Method → action performed by an object
25.2 Class and Object
Suppose you want to describe a student. A student has a name, an age, and a course. These are properties. A student can also perform actions, such as displaying information. These are methods.
Public Class Student
Public Name As String
Public Age As Integer
Public Course As String
Public Sub ShowInfo()
MsgBox("Name: " & Name & vbCrLf &
"Age: " & Age & vbCrLf &
"Course: " & Course)
End Sub
End Class
The code above defines a class named Student.
25.3 Creating an Object
After defining a class, you can create an object from it by using the New keyword.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim s1 As New Student
s1.Name = "John Tan"
s1.Age = 20
s1.Course = "Computer Science"
s1.ShowInfo()
End Sub
Here, s1 is an object created from the Student class.
25.4 Properties and Methods
A property stores data, while a method performs an action. In modern OOP practice, properties are often preferred over public fields because they allow better control and validation. :contentReference[oaicite:4]{index=4}
Public Class Employee
Private _name As String
Public Property Name As String
Get
Return _name
End Get
Set(value As String)
_name = value
End Set
End Property
End Class
Using properties makes your code more structured and flexible.
25.5 Constructors
A constructor is a special procedure that runs automatically when an object is created. In Visual Basic, the constructor is written using Sub New. :contentReference[oaicite:5]{index=5}
Public Class Student
Public Name As String
Public Age As Integer
Public Sub New(studentName As String, studentAge As Integer)
Name = studentName
Age = studentAge
End Sub
End Class
Dim s1 As New Student("Alice", 19)
This makes object creation easier and more organized.
25.6 Inheritance
Inheritance allows one class to reuse the features of another class. The new class is called the child class, while the original class is called the parent class. In Visual Basic, inheritance uses the Inherits keyword. :contentReference[oaicite:6]{index=6}
Public Class Person
Public Name As String
Public Sub ShowName()
MsgBox("Name: " & Name)
End Sub
End Class
Public Class Teacher
Inherits Person
Public Subject As String
End Class
Here, the Teacher class inherits the Name property and the ShowName() method from the Person class.
25.7 Polymorphism
Polymorphism means the same method name can behave differently in different classes. In Visual Basic, this is commonly done with Overridable and Overrides. :contentReference[oaicite:7]{index=7}
Public Class Animal
Public Overridable Sub MakeSound()
MsgBox("Animal sound")
End Sub
End Class
Public Class Dog
Inherits Animal
Public Overrides Sub MakeSound()
MsgBox("Woof!")
End Sub
End Class
This allows child classes to provide their own behavior while keeping the same method name.
Recommended Upgrade
Build on This Foundation
Continue to VB2026
After learning the basics of checkbox controls in VB2015, move to the newest VB2026 tutorial for a more modern VB.NET learning path.
Visual Basic Programming
Use this Top Release book to reinforce your tutorial learning with a more structured guide.
25.8 Why OOP Matters
- A class is a blueprint; an object is an instance.
- Properties store data; methods perform actions.
- Constructors help initialize objects.
- Inheritance allows code reuse.
- Polymorphism allows flexible behavior.