Introduction to Classes and Objects - PowerPoint PPT Presentation

1 / 46
About This Presentation
Title:

Introduction to Classes and Objects

Description:

How to use properties to ensure that only valid data is placed in attributes. ... Middle compartment contains class's attributes or instance variables ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 47
Provided by: pt188
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Classes and Objects


1
Chapter 3
Chapter 4 from the Text Book
  • Introduction to Classes and Objects

2
OBJECTIVES
  • In this chapter you will learn
  • What classes, objects, methods, instance
    variables and properties are.
  • How to declare a class and use it to create an
    object.
  • How to implement a class's behaviors as methods.
  • How to implement a class's attributes as instance
    variables and properties.
  • How to call an object's methods to make them
    perform their tasks.
  • The differences between instance variables of a
    class and local variables of a method.
  • How to use a constructor to ensure that an
    object's attributes are initialized when the
    object is created.
  • The differences between value types and reference
    types.
  • How to use properties to ensure that only valid
    data is placed in attributes.

3
  • 4.1 Introduction
  • 4.2 Classes, Objects, Methods and Instance
    Variables
  • 4.3 Declaring a Class with a Method and
    Instantiating an Object of a Class
  • 4.4 Declaring a Method with a Parameter
  • 4.5 Instance Variables and Properties
  • 4.6 Value Types and Reference Types
  • 4.7 Initializing Objects with Constructors
  • 4.8 Validating Data with Set Accessors in
    Properties4.9

4
4.1 Introduction
  • Classes
  • Attributes
  • Methods
  • Constructors

5
4.2 Classes, Objects, Methods, Properties and
Instance Variables
  • Class provides one or more methods
  • Method represents task in a program
  • Describes the mechanisms that actually perform
    its tasks
  • Hides from its user the complex tasks that it
    performs
  • Method call tells method to perform its task
  • Classes contain one or more attributes
  • Specified by instance variables
  • Carried with the object as it is used

6
4.3 Declaring a Class with a Method and
Instantiating an Object of a Class
  • Class GradeBook
  • Keyword Public is an access modifier
  • Class declarations include
  • Access modifier
  • Keyword Class
  • The corresponding End Class Statement

7
4.3 Declaring a Class with a Method and
Instantiating an Object of a Class (Cont.)
  • Class GradeBook
  • Method declarations
  • Keyword Public indicates method is available to
    the public
  • Keyword Sub indicates that there is no return
    type
  • Access modifier, name of method, parentheses and
    return type, comprise the method header
  • Naming convention is to capitalize the first
    letter of every word

8
Outline
The method header for DisplayMessage()
GradeBook.vb
When method is called, it outputs to screen
9
4.3 Declaring a Class with a Method and
Instantiating an Object of a Class (Cont.)
  • Module GradeBookTest
  • Any class or module that contains a Main method
    can be used to execute an application
  • Visual Basic is extensible
  • Programmers can create new classes
  • Class instance creation expression
  • Keyword New
  • Then name of class to create and parentheses
  • Calling a method
  • Objects name, then dot separator (.)
  • Then methods name and parentheses

10
Outline
GradeBookTest.vb
Use class instance creation expression to create
object of class GradeBook
Call method DisplayMessage using GradeBook object
11
4.3 Declaring a Class with a Method and
Instantiating an Object of a Class (Cont.)
  • UML class diagrams (Fig. 4.3)
  • Top compartment contains name of the class
  • Middle compartment contains classs attributes or
    instance variables
  • Bottom compartment contains classs operations or
    methods
  • Plus sign indicates Public modifier

12
Fig. 4.3 UML class diagram indicating that
class GradeBook has a publicDisplayMessage
operation.
13
4.4 Declaring a Method with a Parameter
  • Method parameters (Fig. 4.4)
  • Additional information passed to a method
  • Supplied in the method call with arguments
  • Input method (Fig. 4.5)
  • Console.ReadLine reads a line of input

14
Outline
Method header DisplayMessage that takes a
courseName argument of type String
GradeBook.vb
Output message with the argument when method is
called
15
Outline
GradeBookTest.vb
Call ReadLine method to read a line of input and
assigns it to nameOfCourse
Call DisplayMessage with an argument
16
4.4 Declaring a Method with a Parameter (Cont.)
  • Parameters specified in methods parameter list
  • Part of method header
  • Uses a comma-separated list
  • Keyword ByVal
  • The argument is passed by value
  • UML class diagram (Fig. 4.6)
  • Parameters specified by parameter name followed
    by a colon and parameter type

17
Fig. 4.6 UML class diagram indicating that
class GradeBook has a DisplayMessage operation
with a courseName parameter of type String.
18
4.5 Instance Variables and Properties
  • Variables declared in the body of method
  • Called local variables
  • Can only be used within that method
  • Variables declared in a class declaration
  • Called fields or instance variables
  • Each object of the class has a separate instance
    of the variable

19
Outline
Instance variable courseNameValue
GradeBook.vb
20
4.5 Instance Variables and Properties (Cont.)
  • Predefined constant identifiers
  • vbCrLf
  • Represents a combination of carriage return and
    linefeed character
  • Outputting this constants value causes
    subsequent text to display at the beginning of
    the next line
  • vbTab
  • A constant that represents a Tab character

21
4.5 Instance Variables and Properties (Cont.)
  • Private keyword
  • Used for most instance variables
  • Private variables and methods are accessible only
    to methods of the class in which they are
    declared
  • Declaring instance variables Private is known as
    information hiding

22
Software Engineering Observation 4.1
  • Precede every instance variable declaration,
    method declaration and property declaration with
    an access modifier. In most cases, instance
    variables should be declared Private, and methods
    and properties should be declared Public. If
    these modifiers are omitted, instance variables
    are Private by default, and methods and
    properties are Public by default. (We will see in
    Section 9.2 that it is appropriate to declare
    certain methods and properties Private if they
    should be accessed only by other methods and
    properties of the class.)

23
Software Engineering Observation 4.2
  • Declaring the instance variables of a class as
    Private and the methods of the class as Public
    facilitates debugging, because problems with data
    manipulations are localized to the classs
    methods and properties.

24
4.5 Instance Variables and Properties (Cont.)
  • Property Declaration
  • Declaration consist of an access modifier,
    keyword Property, name with parentheses, and type
  • Get and Set allows you to access and modify
    private variables outside of the class,
    respectively
  • Contain a Get accessor, Set accessor, or both
  • After defining a property, you can use it like a
    variable
  • ( object_Name.Property_Name )
  • Get and Set Accessors
  • Get accessor contains a Return statement
  • Set accessor assigns a value to its corresponding
    instance variable

25
4.5 Instance Variables and Properties (Cont.)
  • Default initial value
  • Provided for all fields not initialized
  • 0 for numeric/value type variables
  • Nothing for Strings and reference types

26
Outline
GradeBookTest.vb
Calls the Get accessor of property CourseName
Calls the Set accessor of property CourseName
Calls the Get accessor of property CourseName
27
4.5 Instance Variables and Properties (Cont.)
  • UML Diagram
  • Model properties in the UML as attributes
  • Public is indicated by the sign
  • ltltPropertygtgt
  • Propertys name propertys type
  • If the property only contains a Get accessor,
    then place ReadOnly after the propertys type
  • Modeling Private instance variables that are not
    properties
  • Attributes name attributes type
  • Private is indicated by the - sign

28
Fig. 4.9 UML class diagram indicating that
class GradeBook has a courseNameValue attribute
of type String, one property and one method.
29
4.5 Instance Variables and Properties (Cont.)
  • Public variable can be read or written by any
    property or method
  • Private variables can only be access indirectly
    through the classs non-Private properties
  • Class able to control how the data is set or
    returned
  • Allows for data validation
  • Properties of a class should use classs own
    methods to manipulate the classs Private
    instance variables
  • Creates more robust class

30
Software Engineering Observation 4.3
  • Accessing Private data through Set and Get
    accessors not only protects the instance
    variables from receiving invalid values, but also
    hides the internal representation of the instance
    variables from that classs clients. Thus, if
    representation of the data changes (often, to
    reduce the amount of required storage or to
    improve performance), only the properties
    implementations need to changethe clients
    implementations need not change as long as the
    services provided by the properties are preserved.

31
4.6 Value Types vs. Reference Types
  • Types in Visual Basic
  • Value (primitive types except String)
  • Contains a value of that type
  • List of Primitive Types in Appendix L
  • Reference (sometimes called non-primitive types)
  • Objects
  • Default value of Nothing
  • Used to invoke an objects methods and properties

32
Fig. 4.10 Value type variable.
33
Fig. 4.11 Reference type variable.
34
4.7 Initializing Objects with Constructors
  • Constructors
  • Initialize an object of a class
  • Visual Basic requires a constructor for every
    class
  • Visual Basic will provide a default no-argument
    constructor ONLY when none is provided
  • Called when keyword New is followed by the class
    name and parentheses
  • Constructors can also take arguments
  • Constructor header similar to regular Sub method
    header except the name is replaced with keyword
    New

35
Outline
GradeBook.vb
36
Outline
GradeBookTest.vb
37
4.9 Initializing Objects with Constructors (Cont.)
  • UML class diagram
  • Constructors go in third compartment
  • Place ltltconstructorgtgt before New and its
    arguments
  • By convention, place constructors first in their
    compartment

38
Fig. 4.14 UML class diagram indicating that
class GradeBook has a constructor that has a
name parameter of type String.
39
4.8 Validating Data with Set Accessors in
Properties
  • Validations should be made in the Set accessor to
    check if the data is valid
  • By default, the Get and Set accessor has the same
    access as the property, however they can vary.
  • String
  • Length property returns the number of characters
    in the String
  • Substring returns a new String object created by
    copying part of an existing String object
  • To display a double quote, use two double quotes
    in a row

40
Error-Prevention Tip 4.1
  • Unless default initialization of your classs
    instance variables is acceptable, provide a
    constructor to ensure that these variables are
    properly initialized with meaningful values when
    each new object of your class is created.

41
Outline
GradeBook.vb (1 of 2 )
Uses the validation provided in the Set accessor
42
Outline
GradeBook.vb (2 of 2 )
value is invalid take substring!
43
Outline
GradeBookTest.vb (1 of 2 )
44
Outline
GradeBookTest.vb (2 of 2 )
45
Error-Prevention Tip 4.2
  • The benefits of data integrity are not automatic
    simply because instance variables are made
    Privatethe programmer must provide appropriate
    validity checking and report the errors.

46
Error-Prevention Tip 4.3
  • Set accessors that set the values of Private data
    should verify that the intended new values are
    proper if they are not, the Set accessors should
    place the Private instance variables into an
    appropriately consistent state.
Write a Comment
User Comments (0)
About PowerShow.com