Ch10 Object Oriented
Ch10 Object Oriented
Programming
1
Chapter
Object Oriented
10
2
Introduction
◼ Classes
◼ Abstract Data Types
◼ Exceptions
◼ Collections
◼ Object Browser
◼ Scrollable Controls
3
Classes and Objects
5
Classes
• A class is a program structure that defines an
abstract data type
• Must create the class first
• Then can create instances of the class
• Class instances share common attributes
• VB forms and controls are classes
• Each control in the toolbox represents a class
• Placing a button
on a form creates
an instance, or
object, of the class
6
Class Properties, Methods, &
Events
• Programs communicate with an object using the
properties and methods of the class
• Class properties example: Buttons have Location,
Text, and Name properties
• Class methods example: The Focus method
functions identically for every single button
• Class event procedures: Each button in a form
has a different click event procedure
7
Object Oriented Design
• The challenge is to design classes that effectively
cooperate and communicate
• Analyze application requirements to determine
ADTs that best implement the specifications
• Classes are fundamental building blocks
• Typically represent nouns of some type
• A well-designed class may outlive the application
• Other uses for the class may be found
8
Object Oriented Design Example
Specifications:
We need to keep a list of students that lets us track the
courses they have completed. Each student has a transcript
that contains all information about his or her completed
courses. At the end of each semester, we will calculate the
grade point average of each student. At times, users will
search for a particular course taken by a student.
• Nouns from the specification above typically
become classes in the program design
• Verbs such as calculate GPA and search become
methods of those classes
9
OOD Class Characteristics
Class Attributes (properties) Operations (methods)
11
Creating a Class
12
Class Declaration
Public Class Student
MemberDeclarations
End Class
13
Member Variables
• A variable declared inside a class declaration
• Syntax:
AccessSpecifer VariableName As DataType
14
Creating an Instance of a Class
• A two step process creates an instance of a class
• Declare a variable whose type is the class
Dim freshman As Student
15
Accessing Members
• Can work with Public member variables of a
class object in code using this syntax:
objectVariable.memberVariable
• For example:
• If freshman references a Student class object
• And Student class has public member variables
strFirstName, strLastName, and strID
• Can store values in member variables with
freshman.strFirstName = "Joy"
freshman.strLastName = "Robinson"
freshman.strId = "23G794"
16
Property Procedure
• A property procedure is a function that defines a
property
• Controls access to property values
• Procedure has two sections: Get and Set
• Get code executes when value is retrieved
• Set code executes when value is stored
• Properties almost always declared Public to
allow access from outside the class
• Set code often provides data validation logic
17
Property Procedure Syntax
Public Property PropertyName() As DataType
Get
Statements
End Get
Set(ParameterDeclaration)
Statements
End Set
End Property
18
Property Procedure Example
Public Class Student
' Member variables
Private sngTestAvg As Single
End Property
End Class 19
Setting and Validating a Property
• TestAverage property is set as shown:
Dim freshman as New Student()
freshman.TestAverage = 82.3
20
Read-Only Properties
• Useful at times to make a property read-only
• Allows access to property values but cannot
change these values from outside the class
• Add ReadOnly keyword after access specifier
Public ReadOnly Property PropertyName() As DataType
Get
Statements
End Get
End Property
• This causes the propertyName to be read-only --
not settable from outside of the class
21
Read-Only Property Example
' TestGrade property procedure
ReadOnly Property TestGrade() As Char
Get
If sngTestAverage >= 90
return "A“
Else If sngTestAverage >= 80
return "B“
Else If sngTestAverage >= 70
return "C“
Else If sngTestAverage >= 60
return "D“
Else
return "F“
End If
End Get
End Property
22
Class Methods
• In addition to properties, a class may also contain
Sub procedures and functions
• Methods are Sub procedures and functions defined
in a class
• Typically operate on data stored in the class
• The following slide shows a Clear method for the
Student class
• Method called with freshman.Clear()
• Method clears member data in the Student class object
referenced by freshman
23
Clear Method for Student Class
Public Class Student
' Member variables
Private strLastName As String 'Holds last name
Private strFirstName As String 'Holds first name
Private strId As String 'Holds ID number
Private sngTestAvg As Single 'Holds test avg
(...Property procedures omitted...)
' Constructor
Public Sub New()
strFirstName = "(unknown)"
strLastName = "(unknown)"
strId = "(unknown)"
sngTestAvg = 0.0
End Sub
(The rest of this class is omitted.)
End Class
26
Introduction to Inheritance
29
The Vehicle Class (Base Class)
• Consider a Vehicle class with the following:
• Private variable for number of passengers
• Private variable for miles per gallon
• Public property for number of passengers
(Passengers)
• Public property for miles per gallon
(MilesPerGallon)
• This class holds general data about a vehicle
• Can create more specialized classes from the
Vehicle class
30
The Truck Class (Derived Class)
• Declared as:
Public Class Truck
Inherits Vehicle
' Other new properties
' Additional methods
End Class
31
Instantiating the Truck Class
• Instantiated as:
Dim pickUp as New Truck()
pickUp.Passengers = 2
pickUp.MilesPerGallon = 18
pickUp.MaxCargoWeight = 2000
Pickup.FourWheelDrive = True
33
Property Override Example
• Vehicle class has no restriction on number of
passengers
• But may wish to restrict the Truck class to two
passengers at most
• Can override Vehicle class Passengers property by:
• Coding Passengers property in derived class
• Specify Overridable in base class property
• Specify Overrides in derived class property
34
Overridable Base Class Property
• Overridable keyword added to base class
property procedure
Public Overridable Property Passengers() As Integer
Get
Return intPassengers
End Get
Set(ByVal value As Integer)
intPassengers = value
End Set
End Property
35
Overridden Derived Class Property
• Overrides keyword and new logic added to
derived class property procedure
Public Overrides Property Passengers() As Integer
Get
Return MyBase.Passengers
End Get
Set(ByVal value As Integer)
If value >= 1 And value <= 2 Then
MyBase.Passengers = value
Else
MessageBox.Show("Passengers must be 1 or 2", _
"Error")
End If
End Set
End Property
36
Overriding Methods
• Overriding a method is similar to a property
• Specify Overridable and Overrides keywords
• An overridable base class method
Public Overridable Sub ProcedureName()
38
ToString Override Example
• Object class ToString method is Overridable
• Vehicle class might override the ToString method
as shown below
' Overriden ToString method
Public Overrides Function ToString() As String
' Return a string representation
' of a vehicle.
Dim str As String
40