0% found this document useful (0 votes)
23 views24 pages

404 VB Chap4shsh

The document provides a comprehensive overview of object-oriented programming concepts in VB.NET, including class declaration, object creation, access specifiers, constructors, destructors, properties, shared members, polymorphism, inheritance, and interfaces. It explains key terms and syntax, as well as examples for better understanding. Additionally, it highlights differences between interfaces and abstract classes, along with a summary of important keywords used in VB.NET.
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)
23 views24 pages

404 VB Chap4shsh

The document provides a comprehensive overview of object-oriented programming concepts in VB.NET, including class declaration, object creation, access specifiers, constructors, destructors, properties, shared members, polymorphism, inheritance, and interfaces. It explains key terms and syntax, as well as examples for better understanding. Additionally, it highlights differences between interfaces and abstract classes, along with a summary of important keywords used in VB.NET.
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
You are on page 1/ 24

➢ Class

How to declare class in VB.net:-


Syntax:-
[Access Specifier] class classname
Code
End class
Creating objects
You can create object using dim statement
Syntax:-
Dim objectname as new classname
AccessSpecifiers describes as the scope of accessibility of an Object and its
members. We can control the scope of the member object of a class using access
specifiers. We are using access specifiers for providing security of our
applications.
Visual Basic .Net provide five access specifiers , they are as follows :
Public
Private
Protected
Friend
Protected Friend
Public :
Public is the most common access specifier. It can be access from anywhere, hat
means there is no restriction on accessability. The scope of the accessibility is
inside class also in outside the class.
Private :
The scope of the accessibility is limited only inside the classes in which they are
decleared. The Private members can not be accessed outside the class and it is
the least permissive access level.
Protected :
The scope of accessibility is limited within the class and the classses derived
(Inherited )from this class.
Friend :
The Friend access specifier can access within the program that contain its
declarations and also access within the same assembly level. You can use friend
instead of Dim keyword.
Protected Friend :
Protected Friend is same access levels of both Protected and Friend. It can access
anywhere in the same assembly and in the same class also the classes inherited
from the same class .
Creating modules
Module is used to divide code into small units
Eg. Module module1
Code
End module

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

Public class maths


public shared PI AS double = 3.1415
ensd class
C=maths.PI

Polymorphism

It means one name multiple forms.


There are two types of polymorphism:
1. Design time
2. Run time
Design time polymorphism is achieved by function overloading.
Run time polymorphism is achieved by function overiding.

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 .

• Inheritance is also known as generalisation.

• Multiple classes may be inherited from a single class.

• Inheritance represents "ISA" relationship.

• 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

Public class ebonus


inherits employee
dim b as integer
property bonus() as integer
get
return b;
end get
set(Byval value as integer)
b=value
end set
end property
end class
dim e1 as new ebonus
e1.nam=”ABC”
e1.sal=5000
e1.bonus=5
textbox1.text=e1.sal+(e1.sal*e1.bonus)/100

NotInheritable (keyword)

• It is known as sealed class.


• If we write 'NotInheritable' keyword then no other class can be derived
from this class. It cannot be used as base class.
• Sometimes we might want to create a class that cannot be subclass.
• 'NonInheritable' keyword is used with those classes which may be updated
later and may create problems.

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 or mustinherit

• In C++, we use abstract keyword to create an abstract class. When class is


not used for creating objects then it is called abstract class.

• Abstract class is used to work as base class only. It provides skeleton to the
derived class.

• It has no actual code.

• An abstract class is a design concept in program development provide a


base upon which other classes are built.
• Abstract class can only specify members that should be implemented by all
inheriting classes.
• If our class contains at least one 'mustoverride' method then our class
should be declared as 'mustinherit' class.

• 'MustInherit' keyword is used to create a class as abstract class.

Syntax :

Public MustInherit class classname


..
..
logic
End Class

Example:
Public MustInherit class C1

MustOverride sub add(Byval A as integer, Byval B as integer)

Sub display()
msgbox(“Hello”)
End Sub
End Class

Public class C2
Inherits C1

Public overrides sub add(Byval A as integer, Byval B as integer)


msgbox(a+b)
End Sub
End Class

• Method returning abstract classes must override are automatically appear


in the derived class.

➢ Mustoverride

• It indicates that specified method or property has to be implemented in


derived class.
• No other statement are allowed and there may be no 'End sub' or 'End
function' statement.
• 'Mustoverride' method must be declared in 'MustInherit' classes.
• If your class contains any one method as 'MustOverride' keyword then your
class must have 'MustInherit' keyword.
• 'MustOverride' method automatically appears in the derived class.

Example :
As above

➢ Overriding

• The overriding concept is used in inheritance.

• 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.

• Derived class inherits method from its base class.

• If inherited property or method needs to behave differently in the derived


class then it can be overridden.

• We can define a new implementation of method in the derived class.

• The overridable keyword is used to mark a function as overridable. We can


redefine overridable methods in derived class.

• The overridable keyword is used when method of an inherited(Base) class


as in overridable by inheriting class(Derived).
• The keyword 'overrides' is used to make a function which is overriding
some base class function. It means function is overriding base class method.

• It allows to redefine the property or method of inherited class.

Example :
Public class C1
overridable sub display()
msgbox(“Base Class”)
End Sub
End Class

Public Class C2
Inherits C1

Overrides sub display()


msgbox(“Derieved class)
End Sub
end class

dim C as new C2
C.display()

➢ Notoverridable

• It prevents a property or method from being overridden in an inheriting


class.

• Public methods are notoverridable by default.


• If you do not write any keyword before function and procedure then they
are default not overridable.

• Constructor in derived class or inheriting constructors.

❖ 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.

• We can call base class constructor in derived class constructor using


mybase.new()

Example :
Public class G1
private X as integer
private Y as integer

sub new()
X=0
Y=0
end sub

sub new(Byval A as integer, By val B as integer)


X=A
Y=B
end sub
end class
Public class G2
Inherits G1
Private Z as integer
sub new()
mybase.new()
Z=0
End Sub

sub new(Byval A as integer, Byval B as integer, Byval C as integer)


mybase.new(A,B)
Z=C
End Sub
End Class

dim obj AS new G2(10,20,30)

❖ Myclass keyword

My class keyword allows you to call overridable method in your class.

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

overrides sub message()


msgbox("Hello India")
End Sub

End Class

dim c as New C2
c.display()
c.newdisplay()

O/P:
Hello India
Hello world

• In display function when we call message at that time derived class


message is called even if base class contains message() because message
method is overridable.

• So to call overridable base class method we have to use myclass keyword.


➢ Interface

• Interface like classes which define a set of properties, methods and events
but unlike classes interface doesn't contain any implementation code.

• Interface represents 'has-a' relationship like A has son B.

• The classes which implements interface must contain code for interface
methods.

• We can implement multiple interfaces. By that we can implement the


concept of multiple inheritance.

• 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

Public class employee


implements person,executive

dim nam as string


dim title as string

Sub setname (Byval fname as string) Implements person.setname


nam=fname
End Sub

Function getname() as string Implememts person.getname


return nam
End function

Sub settitle(Byval t as string) Implements executive.settitle


title=t
End Sub

Function gettitle() as string Implements executive.gettitle


return title
End Function
End Class

dim obj new employee


obj.setname(“Rishab”)
obj.settitle(“President”)
msgbox(obj.getname()&””& obj.gettitle())

Difference between Interface and Abstract class

Interface Abstract Class


1. A class may implements several A class may inherits only one abstract
interfaces. class.
2. An interface doesn't provide any An abstract class can provide code
code at all. default code or just definition.

3. Doesn't contain constructor. May contain constructor.


4. Doesn't have any variable. May contain variables.
5. Doesn't have must override May contain override keyword
keyword.

Summary of OOP

1. New() - used for creating constructor.

2. Inherits - used to inherit one class to another.


3. NotInheritable - indicates that class cannot be inherited from.

4. MustInherit - indicates that class must be inherited by another class.

5. Overridable - used for base class method which will be override by derived class
method.

6. Overrides - indicates that method overrides a method of base class.

7. MustOverride - indicates a procedure must be overridden in derived class.

8. NotOverridable - indicates that method cannot be overridden in derived class.

9. Mybase - Allows you to call method of intermediate base class.

10. Myclass - allows you to call overridable method in base class.

11. Implements - used implement interface.

You might also like