Unit 4 2
Unit 4 2
where
Accessmodifier defines the access levels of the class; it has values as - Public,
Protected, Friend, Protected Friend and Private. Optional.
Shadows indicate that the variable re-declares and hides an identically named
element, or set of overloaded elements, in a base class. Optional.
MustInherit specifies that the class can be used only as a base class and that you
cannot create an object directly from it, i.e., an abstract class. Optional.
NotInheritable specifies that the class cannot be used as a base class.
Inherits specifies the base class it is inheriting from.
Implements specify the interfaces the class is inheriting from.
Example
o Add new class file to the project and give proper name (Class1.vb). The
extension of the class is .vb
o Write down the following code in Class1.vb File
o Add windows form name frmclass.vb and write down the following code in
btnclass click event.
Encapsulation
o It is a process to bind data and methods in a unit called class. When we
define class that process itself define the concept of Encapsulation.
o It can protect your data from accidental corruption.
o Rather than defining the data in the form of public, we can declare those
fields as private.
Example
Constructor
o It is a special member function which is used to initialize the object of its
class.
o It is automatically called when object is created.
o In vb.net, The name of constructor is NEW()
o It can be overloaded.
o There are 2 types of constructor such as constructor with arguments and
without arguments.
Example
Add windows form and write down the following code in btnconstructor click event.
Inheritance
o It is a process of creating a new class called derives class from the existing
class called Base Class.
o The existing class is known as parent, base, super class.
o The new class is known as child, derive, sub class.
o It is also known as code reusability.
o It can be created by using inherits keyword.
o A derive class obtain all of the methods, properties, and events of the
base class.
o There are 3 modifier related with inheritance.
Inherits
NotInheritable
MustInherit(Abstract Class)
Syntax
Class <Class Name>
[inherits] Base Class Name
Statements
End Class
Example
Add class file name clsbase and write down the following code
Public Class ClsBase ‘Base Class
Protected a As Integer = 40
Sub disp()
MsgBox("I am Base Class")
End Sub
End Class
Add windows form and write down the following code in btninheritance click
event
Private Sub btninheritance_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btninheritance.Click
Dim x As New clsderive
x.disp() ‘Base Class Method
x.show() ‘Base + derive Class’
End Sub
NotInheritable
o It is known as seal class. If we write NotInheritable keyword in class then no
other class can be derived from this class.
o It gives surety that your class is not become parent of any class.
Example
Create class clsA as NotInheritable and try to derive it in clsB
End Class
End Class
MustInherit (Abstract Class)
o MustInherit Keyword is used to create Abstract Class.
o It specifies that class is intended as a base class.
o It provides Skelton to the derived class. it has no actual code.
o It is not used to create an object.
o If our class contains at least one MustOverride method then our class should
be declare as MustInherit class.
o Methods written in Abstract class as MustOverride are automatically appear
in the derived class.
o Mustoverride methods must be declared in MustInherite Class.
o No other statements are allowed and especially there is No End Sub or End
Function.
Example
End Sub
End Sub
End Class
Add windows form and write down the following code in btnabstactclass_Click event
Polymorphism
Add Class file and write this code (it is the example of function overloading)
Public Class Clsmath
Syntax
Public Interface iface1
….logic
End Interface
Example