Programming Part 7
Programming Part 7
Tinodaishe Chibi
BLUEWAVE ACDEMY Mutare
Object-Oriented Programming (OOP) is a programming paradigm that uses
objects and classes to design and structure software. Here are the key
concepts of OOP with simple definitions and examples in Visual Basic (VB):
Module Program
Sub Main()
Dim myCar As New Car()
myCar.Brand = "Toyota"
myCar.Model = "Corolla"
myCar.DisplayInfo() ' Output: Brand: Toyota, Model: Corolla
End Sub
End Module
2. Encapsulation
• Encapsulation: Bundling data (attributes) and methods (functions) that
operate on the data into a single unit (class), and restricting access to
some of the object's components.
Example:
This code creates a "Person" class that keeps track of age securely. It has a secret "age" box that can
only be changed through a "SetAge" function (like a secret handshake). The "GetAge" function lets you
peek inside the box to see the person's age.
Public Class Person
Private age As Integer
Module Program
Sub Main()
Dim person As New Person()
person.SetAge(25)
Console.WriteLine("Age: " & person.GetAge()) ' Output: Age: 25
End Sub
End Module
3. Inheritance
• Inheritance: A mechanism where a new class inherits properties and
methods from an existing class.
Example:
This code is like saying what animals can do. All animals can Eat (general function). Dogs inherit this
ability from animals, but they can also Bark (special dog function). The program makes a pet dog and
shows it can both Eat and Bark
Public Class Animal
Public Sub Eat()
Console.WriteLine("Eating")
End Sub
End Class
Module Program
Sub Main()
Dim myDog As New Dog()
myDog.Eat() ' Output: Eating
myDog.Bark() ' Output: Barking
End Sub
End Module
4. Polymorphism
• Polymorphism: The ability of different classes to be treated as instances
of the same class through inheritance. It allows methods to do different
things based on the object it is acting upon.
Example:
• The Animal class defines a base MakeSound method with a generic message. It's marked
Overridable to allow subclasses to customize the sound.
• Dog and Cat inherit from Animal, overriding MakeSound with their specific sounds ("Bark"
and "Meow") using the Overrides keyword.
• The program demonstrates polymorphism by using an Animal reference (myAnimal) to assign
either Dog or Cat objects. The overridden MakeSound ensures the correct animal sound is played.
Public Class Animal
Public Overridable Sub MakeSound()
Console.WriteLine("Some sound")
End Sub
End Class
Module Program
Sub Main()
Dim myAnimal As Animal
Module Program
Sub Main()
Dim rect As New Rectangle(5, 10)
Console.WriteLine("Area of rectangle: " & rect.GetArea()) ' Output: Area of r
ectangle: 50
End Sub
End Module
In this example, Shape is an abstract class with an abstract method GetArea.
The Rectangle class inherits Shape and provides an implementation for GetArea.
Module Program
Sub Main()
' Using the parameterized constructor
Dim person1 As New Person("Alice", 30)
person1.DisplayInfo() ' Output: Name: Alice, Age: 30
Contact
Chibi Tinodaishe M
0781081816
Mutare
_-Programming is an art that cannot be replicated and requires practice mixed with passion,