Transcript Week 8

Programming with Visual Basic
.NET
Creating and Using Classes
Lecture # 8
Tariq Ibn Aziz
Compunet Corporation
1
Classes
• A class may contain three type of items:
– Variables (state)
Class Members
– methods (behavior)
– constructors
• variable store the values
• methods provides the logic, that constitutes the
behavior of class
• Constructor initialize the state of a new instance of a
class
Compunet Corporation
2
Method
• A method is essentially a set of program statement
• Each method exist as part of a class
• Visual Basic program contain one or more classes, each
may contain one or more method
• Method may call other methods in the same or a different
class
• No program code can exist outside a method and no
method can exist outside a class
• Note: Main() is also a method
Compunet Corporation
3
Shared and Instance Members
• There are two ways that an item can be a member
of a class
– Shared member
– Instance member
• Shared methods are accessed through the class.
• Non Shared methods, also called instance
methods, are accessed through instances of the
class.
Compunet Corporation
4
Shared Method and Variable
• A Shared method is associated with a class, so no need to
create an instance of that class.
• To call Shared method, use the following syntax:
className.mthName(args)
• Shared variable is associated with a class, so no need to
create an instance of that class.
• To call Shared variable, use the following syntax:
className.variable1
Compunet Corporation
5
Object-Oriented Programming
• In OOP classes are Types
• Objects are Instances of classes
• Objects may have member data or member methods or
both
• The mechanism to create a new Object is called
instantiation
Compunet Corporation
6
Three Principles of OOP
• Three language features are fundamental to object-oriented
programming
– Encapsulation is a combination of Data and Behavior in
one package and hiding the implementation of data from the
user
or
– Encapsulation is the mechanism that associate data with
code that manipulate it
Compunet Corporation
7
Three Principles of OOP
– Inheritance, The original class is often called the base class
or the super class, and the new class is often called the
derived class or the subclass. Inheritance is often referred to
as extending the base class or super class.
Compunet Corporation
8
Three Principles of OOP
– Polymorphism (from the Greek, meaning "many forms", or
something similar) is the quality that allows one name to be
used for more than one (hopefully related) but technically
different purpose
– Method overriding occurs when a class declare a method
having same types of signature as a method by one of its
super class
Compunet Corporation
9
Class in Visual Basic
• The following class is a valid class although its empty
Public Class Person
End Class
Empty Class, i.e.
no members
Compunet Corporation
10
Data in a Class
• A class containing Data (state)
class Students
Dim ID As Integer
Dim Name As String
Dim Email As String
End class
Compunet Corporation
Class with
member data
11
Data and Method in a Class
• A class containing Data (state) and Method (Behavior)
class
Dim
Dim
Dim
Sub
Students
ID As Integer
Name As String
Email As String
print()
System.Console.Write (ID)
System.Console.Write (Name)
System.Console.Write (Email)
End Sub
End class
Compunet Corporation
Class with
member data and
member method
12
Creating and Using Class
class Person
Public Dim Name As String
Sub print()
Class with
member data and
member method
System.Console.Write (Name)
End Sub
End class
Public Module M1
Sub Main()
Dim v As New Person
Using class in
MODULE
program
v.Name = "Tariq Aziz"
v.print()
End Sub
End Module
Compunet Corporation
13
Creating and Using Class
class Person
Public Dim Name As String
Sub print()
Class with
member data and
member method
System.Console.Write (Name)
End Sub
End class
Class M1
Shared Sub Main()
Dim v As New Person
Using class in
another class
v.Name = "Tariq Aziz"
v.print()
End Sub
End Class
Compunet Corporation
14
Instance Variables in a Class
• Each object has its own copy of all instance variables
defined by its class
Person class
Object
Dim v As New Person
v
Public Dim Name As String
Sub print()
System.Console.Write (Name)
End Sub
Reference to
Object
Compunet Corporation
Instance variable &
method
15
Instance Variables in a Class
• Instance variables are initialized to defaults values during
the creation of object. Variable of boolean set to false.
Numbers are set to zeros. Any variable that acts as an
object reference is set to null
class Person
Public ID As Integer
Public Name As String
Public Married As Boolean
End class
Dim v As New Person
Person class
Object
Public ID As Integer=0
Public Name As String=null
Public Married As Boolean=False
Compunet Corporation
16
Instance Variables Example
class A
Public Dim x As Double
Output:
Public Dim y As Boolean
p.x = 0
Public Dim s As String
p.y = False
End Class
p.s =
class B
shared Sub Main()
Dim p As New A
System.Console.WriteLine(" p.x = " & p.x)
System.Console.WriteLine(" p.y = " & p.y)
System.Console.WriteLine(" p.s = " & p.s)
End Sub
Compunet Corporation
End class
17
Instance method Example (Contd.)
Class A
Public Dim x As Double
Public Dim y As Boolean
Public Dim s As String
Sub Display()
System.Console.WriteLine( "x=" & x)
System.Console.WriteLine( "y=" & y)
System.Console.WriteLine( "s=" & s)
End Sub
End Class
Class B
shared Sub main()
Dim p As New A
p.Display()
End Sub
End class
Compunet Corporation
18
Output:
x=0
y = False
s=
Shared Variables in a Class
• A Shared variable is shared by all objects of its
class and thus relates to the class itself
•
Shared var1 As Type
• Shared variables are initialized to defaults values
when the class is loaded into memory. Variable of
boolean set to false. Numbers are set to zeros.
Any variable that acts as an object reference is set
to null
Compunet Corporation
19
Shared Variables Example
class A
Public Shared Dim x As Double
Public Shared Dim y As Boolean
Public Shared Dim s As String =
End Class
class B
shared Sub main()
System.Console.WriteLine ("x
System.Console.WriteLine ("y
System.Console.WriteLine ("s
End Sub
End class
Compunet Corporation
"T"
Output:
x=0
y = False
s=T
= " & A.x)
= " & A.y)
= " & A.s)
20
Shared Method Example
class A
Public Shared Dim x As Integer = 10
Public Shared Dim y As Boolean = True
Public Shared Dim s As String ="CA121"
Shared Sub Display()
System.Console.WriteLine(" x = " & x)
System.Console.WriteLine(" y = " & y)
System.Console.WriteLine(" s = " & s)
End Sub
End Class
class B
Output:
shared Sub main()
x = 10
A.Display()
y = True
End Sub
Compunet Corporation
End class
s = CA121
21
Exercise (Classes)
• Write an application that declares a class named Person. It
should have instance variable to record name, age, and
salary. These should be of types String, integer, and Single.
Use the new operator to create a Person object. Set and
display its instance variable.
• Write an application that declares a class named Sphere. It
should have instance variables to record its radius and the
coordinates of its center. The should be of type Double.
Use the new operator to create a Sphere object. Set and
display its instance variable.
Compunet Corporation
22
Polymorphism
• A class may have several Method with same name, it is
known as Method overloading/Polymorphism
• Each Method can have same name, but parameters list
must be different
• The println() is a good example of this concept. It has
several overloaded forms, Each of these accepts one
argument of a different type. The type may be Boolean,
Char, Integer, Long, Single, Double, String or Object. It
is much convenient for programmer to remember one
method name rather than several different ones
• Method overloading provides an easy way to handle
default parameter values
Compunet Corporation
23
Polymorphism in Instance Method
Example 1
Class Point3D
Public Dim x, y As Integer
Sub move(a As Integer)
x = a
End Sub
Sub move(a As Integer, b As Integer)
x = a
y = b
End Sub
End Class
class Point3DConstructor
Shared Sub Main()
Dim p As new Point3D
p.move(100)
System.Console.WriteLine(p.x & " " & p.y)
p.move(200,300)
System.Console.WriteLine(p.x & " " & p.y)
End Sub
End Class
Compunet Corporation
OUTPUT:
100 0
200 300
24
Polymorphism in Instance Method
Example 2
class Point3D
Public Dim x, y As Double
Polymorphism
sub Point3D(a as Double, b as Double)
Example
x=a
y=b
System.Console.WriteLine ("x=" & x & "y=" & y)
End Sub
sub Point3D(a as Double)
x=a
y=200
System.Console.WriteLine ("x=" & x & "y=" & y)
End Sub
Compunet Corporation
25
Polymorphism in Instance Method
Example 2
Shared Sub main()
Dim p As New Point3D
p.point3d(1.0,2.0)
p.point3d(100.0)
p.x = 1.1
p.y = 3.4
System.Console.WriteLine(" p.x = " & p.x)
System.Console.WriteLine(" p.y = " & p.y)
End Sub
End class
Compunet Corporation
26
Polymorphism in Constructor
Method
class Point3D
Public Dim x, y As Integer
Sub New()
x = 100
End Sub
Sub New( a As Integer, b As Integer )
x = a
y = b
End Sub
End Class
class Point3DConstructor
Shared Sub Main()
Dim p As new Point3D
System.Console.WriteLine( p.x & " " & p.y)
Dim q As New Point3D(200,300)
System.Console.WriteLine( q.x & " " & q.y)
End Sub
End Class
Compunet Corporation
27
Exercise
• Write an application that defines a Circle class with two
Method. The first form accepts a double value that
represents the radius of the circle. This method assumes
that the circle is centered at the origin. The second form
accepts three double values. The first two arguments
defines the coordinates of the center, and the third
argument defines the radius
Compunet Corporation
28