404 VB Chap4shsh
404 VB Chap4shsh
➢ Constructor
• A constructor is a special member function whose task is to initialize the
objects of its class.
• A constructor is a special function which called automatically when a class is
created.
• In VB.net constructors names is new ()
• A constructor is invoked whenever a object of its associated class is created
• Constructor can be overloaded
Syntax:-
Sub new ()
End sub
Example
Public class student
dim R as integer
dim Sname as string
dim marks as integer
sub new ()
R=0
Sname = ‘ABC””
marks= 0
end sub
sub new (rollno as integer,name as string,m as inetger)
R=rollno
Sname = name
marks= m
end sub
Sub display ()
Msgbox (“ Roll No = “ & R & “ Name=” Sname & “Total marks= “ &
marks)
end sub
end class
dim S1 as new student
S1. display()
dim S2 as new student (5,” Rishi”,150)
S2. display ()
➢ Destructor
• A destructor is a special function which is called automatically when a class
is destroyed.
• A destructor is also known as finalizer because it is the last method run by
class.
• Within a destructor we can place a code to clean up the object it is used ,
releasing resources (connection, dataset).
• Destructor can not be overloaded.
• When an instance is destructed, the destructors in its inheritance chain are
called in order from most derived to least derived.
Syntax:-
overrides protected sub finalize()
(logic)
end sub
❖ Properties
• A property in VB.net is a procedure like functions, subs.
• Property procedures are executed when property doesn’t have any storage
location
• When users creates objects , user , interacts with the properties of the class
not the data members.
• Generally , for each data member we create property.
There are three types of property
1. Read only (count)
2. Write only
3. Read & Write (text)
Syntax:-
[Accessibility ] Property Propertyname (argument) as data type
Get
return variable
end get
SET ( ByVal new Var as Datatype)
Var = new Var
end set
end property
Example:-
Public class student
dim R as integer
dim Sname as string
Property rollno() as integer
get
return r
end get
Set (ByVal VAl as integer)
r = val
end set
end property
Property nam() as string
get
return Sname
end get
Set (ByVal value as string)
Sname= value
end set
end property
end class
dim S1 as new student
S1. rollno=5
S1.nam= “Rishabh”
msgbox(S1.rollno)
msgbox(S1.nam)
• We can create read only property using readonly keyword before property.
• We can create write only property by writing writeonly keyword.
Example : readonly property P1() as string
get
return name
end et
end property
Writeonly property P2() as string
set(ByVal value as string)
sname=value
end set
end property
➢ Shared Members
We can create shared data members and functions which are directly used using
classname. This value is shared among all the object of the class. For that
‘shared’ keyword is used.
Example :
Polymorphism
Function Overloading
Function overloading means we can declare more than one function with the
same name but different argument in one class.
The function performs operations on argument list in the function call.
Example :
Public class clsmath
Function add(ByVal i as integer) AS integer
return (i+10)
End Function
Function add(ByVal a as integer, ByVal b as integer) as integer
return a+b
End Function
Function add(ByVal X as integer, ByVal y as integer, Byval Z as integer) as
integer
return x+y+z
End Function
End Class
dim c1 as new clsmath
msgbox(c1.add(5,10)) \\15
msgbox(c1.add(20)) \\30
Inheritance
• Inheritance means creating new class based on existing class.
• The new class is also called as derived class. It contains features of base
class as well as new class .
• The inner class from which we inherited interface and behaviour is known
by parent class or superclass or base class.
• A derived class receives all the method, properties and event of base class.
• In VB.net 'inherits' keyword is used to inherit a new class from base class.
Example:
1) Public class baseclass
..
..
end class
Public class dc
inherits baseclass
..
..
end class
2) Public class employee
dim ename as string
dim salary as integer
property nam() as string
get
retun ename
end get
set(ByVal value as string)
ename=value
end set
end property
Property sal() as integer
get
return salary
end get
set(ByVal Value as integer)
salary=value
end set
end property
end class
NotInheritable (keyword)
Example:
Public NotInheritable class emp
..
..
End Class
When 'NotInheritable' keyword is used, no other class may use inherit keyword
to create a sub-class of our class.
Example :
Public class bonus
Inherits emp
.. ERROR
..
End Class
• Abstract class is used to work as base class only. It provides skeleton to the
derived class.
Syntax :
Example:
Public MustInherit class C1
Sub display()
msgbox(“Hello”)
End Sub
End Class
Public class C2
Inherits C1
➢ Mustoverride
Example :
As above
➢ Overriding
• Function overriding means -when we declare more than one function with
the same name but in different class and the relationship between class is
parent child.
Example :
Public class C1
overridable sub display()
msgbox(“Base Class”)
End Sub
End Class
Public Class C2
Inherits C1
dim C as new C2
C.display()
➢ Notoverridable
❖ Mybase keyboard
• We can use 'MyBase' keyword to call methods of base class in derived class.
• Mybase refers to the immediate base class and its inherited members. It
cannot be used to access private members of base class.
Example :
Public class G1
private X as integer
private Y as integer
sub new()
X=0
Y=0
end sub
❖ Myclass keyword
Example :
Public class c1
sub display()
message()
End Sub
sub newdisplay()
myclass.message()
End Sub
Overridable sub message()
msgbox("Hello World...")
End Sub
End Class
public class c2
inherits c1
End Class
dim c as New C2
c.display()
c.newdisplay()
O/P:
Hello India
Hello world
• Interface like classes which define a set of properties, methods and events
but unlike classes interface doesn't contain any implementation code.
• The classes which implements interface must contain code for interface
methods.
• Interface definitions are enclosed within the Interface ... End Interface
statements.
• If you want to use interface with inner class that implements keyword is
used.
Syntax:
Public Interface interfacename
..
..
End Interface
Example:
Public Interface person
sub setname(Byval pname as string)
function getname() as string
End Interface
Public interface executive
sub settitle (Byval t as string)
function gettitle() as string
End interface
Summary of OOP
5. Overridable - used for base class method which will be override by derived class
method.