Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

programming part 7b

This document serves as a guide to Object-Oriented Programming (OOP) using VB.NET for console applications, outlining key principles such as encapsulation, inheritance, polymorphism, and abstraction. It provides practical examples of creating classes and objects, along with tips for exam preparation in Zimsec Computer Science. The document emphasizes the importance of practice and understanding syntax to succeed in programming.

Uploaded by

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

programming part 7b

This document serves as a guide to Object-Oriented Programming (OOP) using VB.NET for console applications, outlining key principles such as encapsulation, inheritance, polymorphism, and abstraction. It provides practical examples of creating classes and objects, along with tips for exam preparation in Zimsec Computer Science. The document emphasizes the importance of practice and understanding syntax to succeed in programming.

Uploaded by

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

OOP for Dummies:

VB.NET Console
Applications
Programming Part 7B
Chibi Tinodaishe – 0781081816(WhatsApp)
(Simple Notes for Zimsec Computer Science – Module Programming Part 7B)

Chapter 1: What Is Object-Oriented Programming (OOP)?

Definition:
OOP is a programming style based on the concept of “objects” that have properties (data)
and methods (actions). It makes it easier to structure and manage code by mimicking real-
life things.

The 4 Key Principles:

1. Encapsulation: Grouping data (attributes) and methods (functions) in a single unit


(class).

2. Inheritance: Creating new classes that reuse, extend, or modify the behavior of
existing classes.

3. Polymorphism: Allowing methods to do different things based on the object calling


them (method overriding).

4. Abstraction: Hiding complex details and showing only the necessary features of an
object.

Chapter 2: Getting Started with VB.NET Console Applications

What is VB.NET?
VB.NET is a programming language by Microsoft that is easy to learn. Console applications
are programs that run in a command-line window.

Creating a Console App:

• Open Visual Studio.

• Select “Console App (.NET Framework)” in VB.NET.

• Write your code in the provided editor.

• Press F5 to run your application.

Chapter 3: Classes and Objects


Class: A blueprint that defines properties and methods.
Object: An instance of a class.

Example – Creating a Simple “Person” Class:

Module Module1

Sub Main()

' Create a new Person object

Dim person1 As New Person("John", 25)

person1.DisplayInfo()

Console.ReadLine() ' Pause to view output

End Sub

End Module

Public Class Person

' Private variables (Encapsulation)

Private _name As String

Private _age As Integer

' Constructor: used to create objects

Public Sub New(ByVal name As String, ByVal age As Integer)

_name = name

_age = age

End Sub

' A method to display information about the person

Public Sub DisplayInfo()

Console.WriteLine("Name: " & _name)


Console.WriteLine("Age: " & _age)

End Sub

End Class

Key Points:

• The constructor (Sub New) sets up the object when it’s created.

• DisplayInfo() shows the object's data.

Chapter 4: Encapsulation

What Is It?
Encapsulation keeps the data safe inside a class and only allows access via methods
(getters and setters).

Example – Using Properties in VB.NET:

Public Class Student

' Private variables

Private _studentName As String

Private _marks As Integer

' Property for _studentName

Public Property StudentName() As String

Get

Return _studentName

End Get

Set(ByVal value As String)

_studentName = value

End Set

End Property
' Property for _marks

Public Property Marks() As Integer

Get

Return _marks

End Get

Set(ByVal value As Integer)

If value >= 0 And value <= 100 Then

_marks = value

Else

Console.WriteLine("Invalid marks!")

End If

End Set

End Property

End Class

Tip: Use properties to protect your data and control how it is accessed.

Chapter 5: Inheritance

What Is It?
Inheritance allows a class to inherit attributes and methods from another class.

Example – Animal and Dog Classes:

' Base Class

Public Class Animal

Public Overridable Sub Speak()

Console.WriteLine("The animal makes a sound.")

End Sub

End Class
' Derived Class inherits from Animal

Public Class Dog

Inherits Animal

' Override the Speak method

Public Overrides Sub Speak()

Console.WriteLine("The dog barks.")

End Sub

End Class

Usage:
When you call the Speak method on a Dog object, it uses the overridden version.

Chapter 6: Polymorphism

What Is It?
Polymorphism means “many forms” – the same method can behave differently on different
classes.

Example – Continuing from the Animal/Dog Example:

Module Module1

Sub Main()

Dim myAnimal As Animal = New Animal()

Dim myDog As Animal = New Dog() ' Notice: declared as Animal type

myAnimal.Speak() ' Outputs: The animal makes a sound.

myDog.Speak() ' Outputs: The dog barks.

Console.ReadLine()

End Sub

End Module
Key Idea:
Even though both objects are of type Animal, the Dog class’s Speak method is called for
myDog.

Chapter 7: Abstraction

What Is It?
Abstraction hides complex details and shows only the essential features.
How?

• Use abstract classes or interfaces (VB.NET uses “MustInherit” for abstract classes).

Example – Abstract Class for Shapes:

Public MustInherit Class Shape

Public MustOverride Function Area() As Double

End Class

Public Class Circle

Inherits Shape

Private _radius As Double

Public Sub New(ByVal radius As Double)

_radius = radius

End Sub

Public Overrides Function Area() As Double

Return Math.PI * _radius * _radius

End Function

End Class
Public Class Rectangle

Inherits Shape

Private _width, _height As Double

Public Sub New(ByVal width As Double, ByVal height As Double)

_width = width

_height = height

End Sub

Public Overrides Function Area() As Double

Return _width * _height

End Function

End Class

Remember:
Abstract classes help you define a common interface for related classes while hiding the
underlying details.

Chapter 8: Putting It All Together – A Simple Console Application

Example – A Simple School App (Mini-Project):

Module Module1

Sub Main()

' Create a Student object and display info

Dim student1 As New Student

student1.StudentName = "Alice"

student1.Marks = 85

Console.WriteLine("Student Name: " & student1.StudentName)

Console.WriteLine("Student Marks: " & student1.Marks)


' Demonstrate inheritance and polymorphism

Dim animal1 As Animal = New Animal()

Dim animal2 As Animal = New Dog()

animal1.Speak() ' Calls Animal's Speak

animal2.Speak() ' Calls Dog's Speak

Console.ReadLine()

End Sub

End Module

Public Class Student

Private _studentName As String

Private _marks As Integer

Public Property StudentName() As String

Get

Return _studentName

End Get

Set(ByVal value As String)

_studentName = value

End Set

End Property

Public Property Marks() As Integer

Get
Return _marks

End Get

Set(ByVal value As Integer)

If value >= 0 And value <= 100 Then

_marks = value

Else

Console.WriteLine("Invalid marks!")

End If

End Set

End Property

End Class

Public Class Animal

Public Overridable Sub Speak()

Console.WriteLine("The animal makes a sound.")

End Sub

End Class

Public Class Dog

Inherits Animal

Public Overrides Sub Speak()

Console.WriteLine("The dog barks.")

End Sub

End Class

Note: This mini-project shows a bit of everything: creating objects, using properties, and
demonstrating inheritance/polymorphism.
Chapter 9: Exam Tips for Zimsec Computer Science

• Understand Concepts:
Make sure you know the four pillars (Encapsulation, Inheritance, Polymorphism,
Abstraction).

• Practice Code:
Write and run small programs—modify examples to see different outcomes.

• Know the Syntax:


Be comfortable with VB.NET syntax for defining classes, properties, methods, and
constructors.

• Review Past Questions:


Look at exam questions related to OOP and try to code the solutions.

• Keep It Simple:
In exams, clarity matters. Use clear naming for classes and methods.

Chapter 10: Conclusion and Further Reading

Congratulations! You now have a concise set of notes on OOP using VB.NET for console
applications. Remember, practice is key. Experiment with these examples and try building
your own small projects. For further reading, check out:

• Microsoft’s official VB.NET documentation.

• Online tutorials and forums like Stack Overflow.

These notes are designed to get you exam-ready. Stay focused, code often, and you’ll ace
those Zimsec Computer Science questions!

You might also like