Download presentation
Presentation is loading. Please wait.
1
Compunet Corporation1 Programming with Visual Basic.NET Creating and Using Classes Lecture # 8 Tariq Ibn Aziz
2
Compunet Corporation2 Classes A class may contain three type of items: –Variables (state) –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 Class Members
3
Compunet Corporation3 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
4
Compunet Corporation4 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.
5
Compunet Corporation5 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
6
Compunet Corporation6 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
7
Compunet Corporation7 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
8
Compunet Corporation8 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.
9
Compunet Corporation9 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
10
Compunet Corporation10 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
11
Compunet Corporation11 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 Class with member data
12
Compunet Corporation12 Data and Method in a Class A class containing Data (state) and Method (Behavior) class Students Dim ID As Integer Dim Name As String Dim Email As String Sub print() System.Console.Write (ID) System.Console.Write (Name) System.Console.Write (Email) End Sub End class Class with member data and member method
13
Compunet Corporation13 Creating and Using Class class Person Public Dim Name As String Sub print() System.Console.Write (Name) End Sub End class Public Module M1 Sub Main() Dim v As New Person v.Name = "Tariq Aziz" v.print() End Sub End Module Class with member data and member method Using class in MODULE program
14
Compunet Corporation14 Creating and Using Class class Person Public Dim Name As String Sub print() System.Console.Write (Name) End Sub End class Class M1 Shared Sub Main() Dim v As New Person v.Name = "Tariq Aziz" v.print() End Sub End Class Class with member data and member method Using class in another class
15
Compunet Corporation15 Instance Variables in a Class Each object has its own copy of all instance variables defined by its class Dim v As New Person Public Dim Name As String Sub print() System.Console.Write (Name) End Sub v Person class Object Reference to Object Instance variable & method
16
Compunet Corporation16 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 As New Person Public ID As Integer=0 Public Name As String=null Public Married As Boolean=False v Person class Object
17
Compunet Corporation17 Instance Variables Example class A Public Dim x As Double Public Dim y As Boolean Public Dim s As String End Class 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 End class Output: p.x = 0 p.y = False p.s =
18
Compunet Corporation18 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 Output: x = 0 y = False s =
19
Compunet Corporation19 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
20
Compunet Corporation20 Shared Variables Example class A Public Shared Dim x As Double Public Shared Dim y As Boolean Public Shared Dim s As String = "T" End Class class B shared Sub main() System.Console.WriteLine ("x = " & A.x) System.Console.WriteLine ("y = " & A.y) System.Console.WriteLine ("s = " & A.s) End Sub End class Output: x = 0 y = False s = T
21
Compunet Corporation21 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 shared Sub main() A.Display() End Sub End class Output: x = 10 y = True s = CA121
22
Compunet Corporation22 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.
23
Compunet Corporation23 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
24
Compunet Corporation24 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 OUTPUT: 100 0 200 300
25
Compunet Corporation25 Polymorphism in Instance Method Example 2 class Point3D Public Dim x, y As Double sub Point3D(a as Double, b as Double) 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 Polymorphism Example
26
Compunet Corporation26 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
27
Compunet Corporation27 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
28
Compunet Corporation28 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.