FSB23103 Object Oriented Programming

UNIVERSITI KUALA LUMPUR Malaysia France Institute

Lecture 4 Classes and objects (1) Mdm Ratnawati Ibrahim

FSB23103

1

Topics
• • • • • What are objects? What are classes? Encapsulation Dot notation Example – bank account class

UNIVERSITI KUALA LUMPUR Malaysia France Institute

FSB23103

2

Objects (1)
• In the real world, objects are everywhere; human think in terms of objects  e.g. people, animals, cars, trees, students, courses, circles, triangles • All objects  have attributes
• e.g. a person’s name, gender, age, weight, height

UNIVERSITI KUALA LUMPUR Malaysia France Institute

 exhibit behaviours
• e.g. a car accelerates, brakes, turns

• Human learn about objects by studying their attributes and observe their behaviours

FSB23103

3

Objects (2)
• An object has  values associated with it to describe its characteristics or properties
• e.g. a car’s registration no., type, colour a module’s title, convener

UNIVERSITI KUALA LUMPUR Malaysia France Institute

 actions it might perform or can be performed on it
• e.g. a ball rolls, bounces, inflates, deflates a dog barks, walks, sleeps

FSB23103

4

Classes
• Objects may be categorised into a generic group or class, e.g.  a person is a member of the class People  a car is an instance of the class Car  a ball is an object of the class Circle • Objects of a class have the same characteristics and behaviours • In computer science, a class is also referred to as an Abstract Data Type (ADT)

UNIVERSITI KUALA LUMPUR Malaysia France Institute

FSB23103

5

Classes and objects - Example
• Three classes and their objects  Circle
• sun

UNIVERSITI KUALA LUMPUR Malaysia France Institute

 Triangle
• roof , tree canopy

 Rectangle
• sky, ground, wall, window, door

Using Shapes

FSB23103

6

Object Oriented Programming (OOP)
• OOP is based on the intuitive concept of an object in the real world • It models objects by their attributes, behaviours and interrelationships • OOP languages including VB.Net, Java, C++

UNIVERSITI KUALA LUMPUR Malaysia France Institute

FSB23103

7

Programming concepts
• In a program, performing a task requires a method to describe the mechanisms of how to perform the task  e.g. move the Sun up or down • The method hides these mechanisms from its users  user does not need to know implementation details in order to use the method – information hiding or encapsulation
• just like you can use the brake to stop the car without knowing how it works mechanically

UNIVERSITI KUALA LUMPUR Malaysia France Institute

• To perform a task, a message is sent to the object to call the method • In OOP, a unit called Class provide one or more methods that are designed to perform the class’s tasks  e.g. the class Circle, Triangle and Rectangle
FSB23103

8

Objects vf classes
• A classes is to an object as a blueprint to a house  a class is a ‘plan’ for building objects of the class  many objects of one class may be created
• just like many houses can be built from one blueprint

UNIVERSITI KUALA LUMPUR Malaysia France Institute

• Objects of the same class  possess the same properties, but may have different values for specific properties
• e.g. objects of Car class: a red car, or a blue car

 are capable of the same actions
• all car can accelerate, brake, turn

 must be instantiated or created before they can be used
• just like a real car must be built before you can drive it!
FSB23103

9

Example class - Account
• A class named Account represents bank account • It provides ways to  record current balance  create a new account
• e.g. accountA and accountB

UNIVERSITI KUALA LUMPUR Malaysia France Institute

 deposit a given amount of money  withdraw a given amount of money  get current balance

FSB23103

10

Example program - bank accounts
• e.g. Transfer money from bank account A to account B • Both account A and B  possess the same property, but may have different values
• e.g. current balance

UNIVERSITI KUALA LUMPUR Malaysia France Institute

 are capable of the same actions
• e.g. deposit, withdraw, show current balance

• They are objects or instances of the Account class
Bank Accounts

FSB23103

11

Class definition
• A class is defined in terms of its  state  behaviour or actions • Each class contains  data to describe the state, and  a set of methods that manipulate the data • Class implementation details are normally hidden from the users. This is termed information hiding or encapsulation

UNIVERSITI KUALA LUMPUR Malaysia France Institute

FSB23103

12

Class state
• defines characteristics of a class • is described by its attributes, i.e. class instance variables • e.g.  Account class: balance  Button class: name, text, foreColor, size  Account class: xpos, ypos, radius  Student class: studID, name, DoB, gender

UNIVERSITI KUALA LUMPUR Malaysia France Institute

FSB23103

13

Class behaviour
• defines actions that a class can perform or can be performed on • is described by class methods • e.g.  Deposit and Withdraw methods of Account class Note: An object of the class must be instantiated or created before it can be used to perform the actions

UNIVERSITI KUALA LUMPUR Malaysia France Institute

FSB23103

14

Example – Account class
• The Account class:
Class instance variable
UNIVERSITI KUALA LUMPUR Malaysia France Institute

Public Class Account Private balance As Double

Class constructor

Public Sub New(ByVal iniAmount As Double) balance = iniAmount End Sub Public Sub Deposit(ByVal amount As Double) balance += amount x -= 1 Mutator Method End Sub is the same as x = x - 1 Public Sub Withdraw(ByVal amount As Double) i.e. decrement x by 1 balance -= amount End Sub Public ReadOnly Property currentBalance() As Double Get Return balance Assessor End Get method End Property End Class
FSB23103

Note: x += 1 is the same as x = x + 1 i.e. increment x by 1

15

Class method types
• Constructor  to create an instance or object of the class • Accessor  to access the state of an object of the class • Mutator  to manipulate or change the state of an object of the class • Other actions

UNIVERSITI KUALA LUMPUR Malaysia France Institute

FSB23103

16

Class members
• Class instances variables - usually Private  store the state of an object • Class constructor  create an object • Class properties  access or change the state of an object • Public methods  change the behaviour of an object • Private methods  only accessible within the class

UNIVERSITI KUALA LUMPUR Malaysia France Institute

FSB23103

17

Class structure
• Class members:  Class instances variables (usually Private)  Public methods & properties
• • • • Constructor Mutators Accessors Other actions

Public Class className Private Class instance variables Public methods
Constructor Other actions Public properties Private methods

UNIVERSITI KUALA LUMPUR Malaysia France Institute

 Private methods
Note: A private variable or method is not accessible outside the class – information hiding or encapsulation
FSB23103

End Class
18

Dot notation
• To refer to a public member (i.e. a method or property) outside the class definition, its name must be preceded by the object’s name and a dot . • e.g.
accountA.Withdraw(100) accountB.Deposit(100)
Dot

UNIVERSITI KUALA LUMPUR Malaysia France Institute

FSB23103

19

Example - Account class
• The class Account code:
Public Class Account ’ class instance variable Private balance As Double

Record current balance Create a new object of the class Deposit Withdraw

UNIVERSITI KUALA LUMPUR Malaysia France Institute

’ class constructor Public Sub New(ByVal iniAmount As Double) balance = iniAmount End Sub Public Sub balance End Sub Public Sub balance End Sub Deposit(ByVal amount As Double) += amount Withdraw(ByVal amount As Double) -= amount

Public ReadOnly Property currentBalance() As Double Get Return balance End Get End Property End Class
FSB23103

Get current balance

20

Example - using Account class (1)
• Transfer money from bank account A to account B  both account A and B are objects or instances of the class Account

UNIVERSITI KUALA LUMPUR Malaysia France Institute

Bank Accounts

FSB23103

21

Example – using Account class (2)
• In design view
UNIVERSITI KUALA LUMPUR Malaysia France Institute

• Two output lables
lblAccountA lblAccountB

• One input text box
txbAmount

• Two command buttons
cmdTransfer cmdExit

FSB23103

22

Example - using Account class (3)
• Program code:
Public Class Form1 Dim accountA As Account Dim accountB As Account Declare object variables

UNIVERSITI KUALA LUMPUR Malaysia France Institute

Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load accountA = New Account(2000) accountB = New Account(1000) Create and ShowBalance() initialize objects End Sub Private Sub cmdtransfer_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdTransfer.Click Dim amount As Double = CDbl(txbAmount.Text) accountA.Withdraw(amount) Transfer money accountB.Deposit(amount) from account A ShowBalance() to account B User End Sub input
FSB23103

23

Example - bank account (4)
• Program code:
Private Sub ShowBalance() lblAccountA.Text = CStr(accountA.currentBalance()) lblAccountB.Text = CStr(accountB.currentBalance()) End Sub Private Sub cmdExit_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles cmdExit.Click Close() Exit program End Sub End Class Show balance on the form

UNIVERSITI KUALA LUMPUR Malaysia France Institute

FSB23103

24

Summary
• Classes are described by  state • describes characteristics of a class • is specified by its attributes - class instance variables  behaviour or actions • a set of methods to describe actions that a class can perform or can be performed on Class members:  class instance variables  class constructor  mutator methods  accessor methods  other action methods Objects of a class  possess the same characteristics or properties  are capable of the same actions  must be instantiated (created) before they can be used OOP – Object Oriented Programming
FSB23103

UNIVERSITI KUALA LUMPUR Malaysia France Institute

25