0% found this document useful (0 votes)
14 views13 pages

Unit 4

Uploaded by

khanakgupta321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views13 pages

Unit 4

Uploaded by

khanakgupta321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

✅ Q8: Object-Oriented Programming Concepts in

VB.NET
🔹 1. Class and Object
●​ Class: Blueprint of an object.​

●​ Object: Instance of a class.​

vb
CopyEdit
Public Class Car
Public Brand As String
Public Sub ShowBrand()
Console.WriteLine("Brand: " & Brand)
End Sub
End Class

Module Module1
Sub Main()
Dim c As New Car()
c.Brand = "Toyota"
c.ShowBrand()
End Sub
End Module
🔹 2. Encapsulation
●​ Wrapping data and methods together.​

●​ Access controlled via properties or access specifiers.​

vb
CopyEdit
Public Class Student
Private _marks As Integer

Public Property Marks As Integer


Get
Return _marks
End Get
Set(value As Integer)
If value >= 0 Then
_marks = value
End If
End Set
End Property
End Class

🔹 3. Abstraction
●​ Showing only essential features, hiding internal details.​

vb
CopyEdit
Public MustInherit Class Shape
Public MustOverride Sub Draw()
End Class

Public Class Circle


Inherits Shape
Public Overrides Sub Draw()
Console.WriteLine("Drawing a Circle")
End Sub
End Class

🔹 4. Inheritance
●​ A class (child) inherits properties and methods of another class (parent).​

vb
CopyEdit
Public Class Animal
Public Sub Eat()
Console.WriteLine("Animal Eats")
End Sub
End Class

Public Class Dog


Inherits Animal
Public Sub Bark()
Console.WriteLine("Dog Barks")
End Sub
End Class

🔹 5. Polymorphism
●​ Same method behaves differently in different classes (overriding/overloading).​

vb
CopyEdit
Public Class A
Public Overridable Sub Show()
Console.WriteLine("Class A Show")
End Sub
End Class

Public Class B
Inherits A
Public Overrides Sub Show()
Console.WriteLine("Class B Show")
End Sub
End Class

✅ Q9: Reusable Components in VB.NET


🔹 Definition
Reusable components are self-contained units (like a class, user control, or DLL) that can be
used in multiple applications without modification.

🔹 Advantages
●​ Reduces development time.​

●​ Increases code maintainability.​

●​ Ensures consistency and standardization.​

🔹 Example: Creating a Reusable Class Library


1.​ Step 1: Open Visual Studio > Create New Project > Class Library (.NET)​

2.​ Step 2: Write the reusable class:​

vb
CopyEdit
Public Class Calculator
Public Function Add(a As Integer, b As Integer) As Integer
Return a + b
End Function
End Class
3.​ Step 3: Build the project → Generates YourProject.dll​

🔹 Step-by-Step: Plugging Component into VS.NET


1.​ Create a Windows Forms App​

2.​ Right-click "References" → Add Reference​

3.​ Browse → Select DLL (your component)​

4.​ Use the component in your code:​

vb
CopyEdit
Dim calc As New Calculator()
Dim result As Integer = calc.Add(10, 20)
MessageBox.Show("Sum = " & result)

🔹 Custom Reusable UI Component (UserControl)


vb
CopyEdit
Public Class MyButton
Inherits Button

Public Sub New()


Me.Text = "Click Me"
Me.BackColor = Color.LightGreen
End Sub
End Class

Build the control and drag it from the Toolbox in Visual Studio.
🔹 1. Class
A class is a blueprint or template from which objects are created.​
It encapsulates data (variables) and functions (methods) into a single unit.

●​ Example: A class named Car can contain properties like brand, color, and methods
like Drive().​

Syntax in VB.NET:

vb
CopyEdit
Public Class Car
Public brand As String
Public Sub Drive()
Console.WriteLine("Driving the car")
End Sub
End Class

🔹 2. Object
An object is an instance of a class. It represents a real-world entity.

●​ Multiple objects can be created from a single class.​

●​ Each object has its own data but shares the class definition.​

Example:

vb
CopyEdit
Dim c1 As New Car()
c1.brand = "Toyota"

🔹 3. Encapsulation
Encapsulation is the process of wrapping data (variables) and code (methods) into a single
unit (class).​
It also allows data hiding using access modifiers (e.g., Private, Public).

●​ Prevents external access to internal class details.​

●​ Access is controlled through properties or methods.​

Benefits:

●​ Security​

●​ Modularity​

●​ Maintainability​

🔹 4. Abstraction
Abstraction means hiding the complex implementation details and showing only essential
features to the user.

●​ Achieved using abstract classes and interfaces in VB.NET.​

●​ Focuses on what an object does, not how it does it.​

Example: A Shape class can have a Draw() method without knowing how each shape is
drawn.

🔹 5. Inheritance
Inheritance is the process by which one class (child) can inherit the properties and methods
of another class (parent).

●​ Promotes code reusability.​

●​ Allows creation of hierarchical relationships.​

Types of Inheritance:
●​ Single​

●​ Multilevel​

●​ Hierarchical (VB.NET does not support multiple inheritance directly)​

Syntax:

vb
CopyEdit
Public Class Animal
Public Sub Eat()
Console.WriteLine("Animal eats")
End Sub
End Class

Public Class Dog


Inherits Animal
End Class

🔹 6. Polymorphism
Polymorphism means "many forms". It allows methods to behave differently based on the
object.

●​ Compile-time Polymorphism: Method Overloading (same method name, different


parameters).​

●​ Run-time Polymorphism: Method Overriding (child class overrides parent method


using Overrides).​

Benefits:

●​ Code flexibility​

●​ Extensibility​
✅ Summary Table
Concept Purpose Achieved Through

Class Blueprint for objects Class keyword

Object Instance of class New keyword

Encapsulation Data protection and hiding Private fields, Public properties

Abstraction Hide implementation, show interface Abstract classes, interfaces

Inheritance Code reuse between classes Inherits keyword

Polymorphism Dynamic behavior based on object Overloading and Overriding

📘 Full VB.NET Program Example


vb

CopyEdit

' VB.NET OOP Concepts Example

' ------------------ ABSTRACTION ------------------

' Abstract base class

Public MustInherit Class Animal

Protected name As String

Public Sub New(ByVal name As String)

Me.name = name
End Sub

' Abstract method

Public MustOverride Sub Speak()

End Class

' ------------------ ENCAPSULATION ------------------

Public Class Pet

Private petName As String ' Private variable

' Public property to access private field

Public Property Name() As String

Get

Return petName

End Get

Set(ByVal value As String)

petName = value

End Set

End Property

End Class

' ------------------ INHERITANCE ------------------

' Dog inherits from Animal


Public Class Dog

Inherits Animal

Public Sub New(ByVal name As String)

MyBase.New(name)

End Sub

' ------------------ POLYMORPHISM ------------------

Public Overrides Sub Speak()

Console.WriteLine(name & " says: Woof!")

End Sub

End Class

' Cat also inherits from Animal

Public Class Cat

Inherits Animal

Public Sub New(ByVal name As String)

MyBase.New(name)

End Sub

Public Overrides Sub Speak()

Console.WriteLine(name & " says: Meow!")


End Sub

End Class

' ------------------ CLASS & OBJECT ------------------

Module Program

Sub Main()

' Creating object of Pet (Encapsulation)

Dim myPet As New Pet()

myPet.Name = "Tommy"

Console.WriteLine("Pet name is: " & myPet.Name)

' Creating objects (Object & Class)

Dim dog1 As New Dog("Bruno")

Dim cat1 As New Cat("Kitty")

' Calling Speak method (Abstraction & Polymorphism)

dog1.Speak()

cat1.Speak()

Console.ReadLine()

End Sub

End Module
🧠 Key Highlights
Concept Where It's Shown

Class Public Class Dog, Cat, Pet, Animal

Object Dim dog1 As New Dog("Bruno"), etc.

Encapsulation Private petName + Public Property Name()

Abstraction MustInherit Class Animal with MustOverride


Speak()

Inheritance Inherits Animal in Dog and Cat classes

Polymorphis Overrides Sub Speak() in both child classes


m

You might also like