Introduction to classes objects
1 / 48

Introduction to Classes & Objects - PowerPoint PPT Presentation

  • Uploaded on

Introduction to Classes & Objects. Chapter 4. Quotes for Today. You will see something new. Two things. And I call them Thing One and Thing Two. Dr. Theordor Seuss Geisel Nothing can have value without being an object of utility. Karl Marx. Classes. Properties, Methods, & Constructors.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' Introduction to Classes & Objects' - achilles-tocci

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Introduction to classes objects

Introduction to Classes & Objects

Chapter 4


Quotes for today
Quotes for Today

You will see something new. Two things. And I call them Thing One and Thing Two.

Dr. Theordor Seuss Geisel

Nothing can have value without being an object of utility.

Karl Marx




Properties, Methods, & Constructors


Methods attributes
Methods & Attributes

  • 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 same as a Procedure

  • Classes contain one or more attributes

    • Specified by instance variables

    • Carried with the object as it is used



  • Is a blueprint or template for one or more objects

  • Defines what the object can do

  • Plenty of pre-defined classes available in Visual Basic

    • Ex. System.MathSystem.Windows.Forms.Form



  • Is a user interface element

  • Can be created on VB form using control in Toolbox

  • Forms are objects

  • Inherit functionality from their class



  • Is a special statement that performs an action or a service for a particular object

    • Syntax Object.Method(Value)

    • Example Math.Min(3,5)



Methods cont

  • 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 add parentheses

  • Calling a method (Class.Method)

    • Object’s name, then dot separator (.)

    • Then method’s name and parentheses


Example of method
Example of Method

' Fig. 4.1: GradeBook.vb

' Class declaration with one method.

Public Class GradeBook‘ Method Header

' display a welcome message to the GradeBook user

Public Sub DisplayMessage()

Console.WriteLine("Welcome to the Grade Book!")

End Sub ' DisplayMessage

End Class ' GradeBook

When Method is called, it is output to screen


Uml diagramming
UML Diagramming

  • Unified Modeling Language

    • Developed by the “three amigos”:

      • Grady Booch – Booch Method

      • James Rumbaugh – OMT (Object Modeling Technique)

      • Ivar Jacobson – OOSE (Object-Oriented Software Engineering)


Uml two goals
UML Two Goals

  • Provide consistency in giving feedback to project sponsor that the problem domain is well understood.

  • Provide a consistent model for proper software implementation.

  • Based on Synergy Model


Uml 9 diagrams
UML 9 Diagrams


Uml class diagrams
UML Class Diagrams

  • Top compartment

    • name of class

  • Middle compartment

    • class’s attributes or instance variables

  • Bottom compartment

    • Class’s operations or methods

    • Plus sign indicates Public modifier


Example of uml diagram
Example of UML Diagram

  • Indicates that class GradeBook has a public DisplayMessage operation.


Declaring methods
Declaring Methods

  • Method parameters

    • Information passed to method

    • Called arguments

    • Supplied in the method call

  • Input method

    • Reads line of input - Console.ReadLine

    • Dim nameOfCourse As String = _Console.ReadLine()


Method with parameter
Method with Parameter

  • Parameters specified in method’s parameter list

    • Part of method header

    • Uses a comma-separated list

    • Keyword ByVal

      • The argument is passed by value

More about ByVal & ByRef later


Uml example
UML Example

  • indicates that classGradeBookhas a DisplayMessageoperation with acourseNameparameter of type String.


Instance variables properties
Instance Variables & 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



Getaccessor for property courseNameValue

Calls the Getaccessor of propertyCourseName

Setaccessor for propertycourseNameValue


' Fig. 4.7: GradeBook.vb

' GradeBook class that contains instance variable courseNameValue

' and a property to get and set its value.

Public Class GradeBook

Private courseNameValue As String ' course name for this GradeBook

' property CourseName

Public Property CourseName() As String

Get ' retrieve courseNameValue

Return courseNameValue

End Get

Set(ByVal value As String)' set courseNameValue

courseNameValue = value ' store the course name in the object

End Set

End Property ' CourseName

' display a welcome message to the GradeBook user

Public Sub DisplayMessage()

' use property CourseName to display the

' name of the course this GradeBook represents

Console.WriteLine("Welcome to the grade book for " _

& vbCrLf & CourseName & "!")

End Sub ' DisplayMessage

End Class ' GradeBook

Instance variable courseNameValue


Predefined constant identifiers
Predefined Constant Identifiers

  • vbCrLf

    • Represents a combination of carriage return and linefeed character

    • Outputting this constant’s value causes subsequent text to display at the beginning of the next line

  • vbTab

    • A constant that represents a Tab character


Private keyword
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


Instance variables properties continued
Instance Variables & Properties continued

  • Property Declaration

    • Declaration consist of an access modifier, keyword Property, name with parentheses, and type

    • GetandSet 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 )


Instance variables properties continued1
Instance Variables & Properties continued

  • Get and SetAccessors

    • Get accessor contains a Return statement

    • Set accessor assigns a value to its corresponding instance variable


Instance variables properties continued2
Instance Variables & Properties continued

  • Default initial value

    • Provided for all fields not initialized

      • 0 for numeric/value type variables

      • Nothing for Strings and reference types


Calls the Getaccessor of propertyCourseName

Calls the Setaccessor of propertyCourseName

Calls the Getaccessor of propertyCourseName

' display initial value of property CourseName (invokes Get)

Console.WriteLine( "Initial course name is: " _

& gradeBook.CourseName& vbCrLf)

‘ prompt for course name

Console.WriteLine("Please enter the course name:")

‘ read course name

Dim theName As String = Console.ReadLine()

gradeBook.CourseName = theName' set the CourseName (invokes Set)

Console.WriteLine() ' output a blank line

‘display welcome message including the course name (invokes Get)


End Sub ' Main

End Module ' GradeBookTest


Uml diagram
UML Diagram

  • Model properties in the UML as attributes:

    • Public is indicated by the “+” sign

    • <<Property>>

    • Property’s name “:” property’s type

    • If the property only contains a Get accessor, then place “{ReadOnly}” after the property’s type

  • Modeling Private instance variables that are not properties:

    • Attribute’s name “:” attribute’s type

    • Private is indicated by the “-” sign


Uml class diagram
UML Class Diagram

  • Indicates:

  • classGradeBookhas acourseNameValueattribute oftype String

  • one public property and

  • one method


Instance variables properties continued3
Instance Variables & Properties continued

  • Public variable

    • can be read or written by any property or method


Instance variables properties continued4
Instance Variables & Properties continued

  • Private variables

    • can only be accessed indirectly through the class’s non-Private properties

    • Class able to control how the data is set or returned

    • Allows for data validation

    • Properties of a class should use class’s own methods to manipulate the class’s Private instance variables

      • Creates more robust class


Value types vs reference types

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 object’s methods and properties






  • Initialize an object of a class

  • Required for every class

  • Provides a default no-argument constructor ONLY when none is provided

  • Called when keyword New is followed by the class name and parentheses

  • Can also take arguments

  • Header similar to Sub method header except name is replaced with keyword New


Constructor example

Call constructor to create first grade book object

Constructor to initialize courseNameValue variable

Create second grade book object

Constructor Example

' Fig. 4.12: GradeBook.vb

' GradeBook class with a constructor to initialize the course name.

Public Class GradeBook

Private courseNameValue As String ' course name for this GradeBook

' constructor initializes course name with String supplied as argument

Public Sub New(ByVal name As String)

CourseName = name ' initialize courseNameValue via property

End Sub ' New

Sub Main()

' create GradeBook object

Dim gradeBook1 As New GradeBook( _

"CS101 Introduction to Visual Basic Programming")

Dim gradeBook2 As New GradeBook( _

"CS102 Data Structures in Visual Basic")


Uml class diagram1
UML Class Diagram

  • Constructors go in third compartment

  • Place “<<constructor>>” before New and its arguments

  • By convention, place constructors first in their compartment


Uml class diagram2
UML class diagram

Indicates that classGradeBookhas a constructor that has anameparameter of typeString.


Validating data with set accessors in properties
Validating Data with Set Accessors in Properties

  • Validations should be made in the Setaccessor to check if the data is valid

  • By default, the Get and Setaccessor 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


Uml class diagrams1
UML Class Diagrams

  • Allows suppression of class attributes and operations

    • Called an elided diagram

  • Solid line that connects two classes represents an association

    • numbers near end of each line are multiplicity values


Multiplicity types
Multiplicity Types


Uml class diagrams2
UML Class Diagrams

  • Solid diamonds attached to association lines indicate a composition relationship

  • Hollow diamonds indicate aggregation – a weaker form of composition


Class diagram for the atm model
Class Diagram for the ATM Model


Composition relationships of a class car
Composition Relationships of a class Car.

Class Diagram


Atm system model including class deposit
ATM System Model including Class Deposit.

Class Diagram



Control Structures part 1