0% found this document useful (0 votes)
11 views

Programming Part 7

Programming

Uploaded by

niftyrue
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Programming Part 7

Programming

Uploaded by

niftyrue
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PROGRAMMING PART 7

OBJECT ORIENTED PROGRAMMING

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):

1. Class and Object


• Class: A blueprint for creating objects. It defines properties and
methods.
• Object: An instance of a class.
Example:
Public Class Car
Public Brand As String
Public Model As String

Public Sub DisplayInfo()


Console.WriteLine("Brand: " & Brand & ", Model: " & Model)
End Sub
End Class

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

Public Sub SetAge(ByVal newAge As Integer)


If newAge > 0 Then
age = newAge
End If
End Sub

Public Function GetAge() As Integer


Return age
End Function
End Class

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

Public Class Dog


Inherits Animal

Public Sub Bark()


Console.WriteLine("Barking")
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

Public Class Dog


Inherits Animal

Public Overrides Sub MakeSound()


Console.WriteLine("Bark")
End Sub
End Class

Public Class Cat


Inherits Animal

Public Overrides Sub MakeSound()


Console.WriteLine("Meow")
End Sub
End Class

Module Program
Sub Main()
Dim myAnimal As Animal

myAnimal = New Dog()


myAnimal.MakeSound() ' Output: Bark

myAnimal = New Cat()


myAnimal.MakeSound() ' Output: Meow
End Sub
End Module
5. Abstraction
• Abstraction: Hiding the complex implementation details and showing
only the necessary features of an object.
Example:
• This code defines a general "Shape" with a secret "GetArea" power (abstract function).
• Rectangles are a kind of shape, so they inherit this power but replace it with their own special
area formula (width times height).
• The program makes a rectangle and uses the "GetArea" power (even though it goes through
the general "Shape" class) to get the rectangle's specific area.
Public MustInherit Class Shape
Public MustOverride Function GetArea() As Double
End Class

Public Class Rectangle


Inherits Shape
Private width As Double
Private height As Double

Public Sub New(ByVal w As Double, ByVal h As Double)


width = w
height = h
End Sub

Public Overrides Function GetArea() As Double


Return width * height
End Function
End Class

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.

A constructor is a special method in a class that is automatically called when


an instance (object) of the class is created. Constructors are used to initialize
the object's properties and perform any setup steps that are required when
the object is instantiated.
Key Points about Constructors:
1. Initialization: Constructors are primarily used to initialize the data
members of the class.
2. Same Name as Class: In Visual Basic, the constructor has the same
name as the class and is defined using the Sub New keyword.
3. No Return Type: Constructors do not have a return type, not even Void.
4. Overloading: You can define multiple constructors with different
parameters (constructor overloading) to provide various ways to
initialize objects.
Example of a Constructor in Visual Basic:
Let's consider a simple example with a Person class that has a constructor to
initialize its properties:
Public Class Person
Public Name As String
Public Age As Integer

' Constructor with parameters


Public Sub New(ByVal name As String, ByVal age As Integer)
Me.Name = name
Me.Age = age
End Sub

' Default constructor (optional)


Public Sub New()
Name = "Unknown"
Age = 0
End Sub

Public Sub DisplayInfo()


Console.WriteLine("Name: " & Name & ", Age: " & Age)
End Sub
End Class

Module Program
Sub Main()
' Using the parameterized constructor
Dim person1 As New Person("Alice", 30)
person1.DisplayInfo() ' Output: Name: Alice, Age: 30

' Using the default constructor


Dim person2 As New Person()
person2.DisplayInfo() ' Output: Name: Unknown, Age: 0
End Sub
End Module

Contact

Chibi Tinodaishe M

[email protected]

0781081816

Mutare

_-Programming is an art that cannot be replicated and requires practice mixed with passion,

You might also like