ObjectOrientedProgramming Converted
ObjectOrientedProgramming Converted
Overview
Object-oriented programming (OOP) is a way to organize
and conceptualize a program as a set of interacting objects.
Examples:
Dog
Attributes: breed, color, hungry, tired, etc.
Behaviors: eating, sleeping, etc.
Bank Account
Attributes: account number, owner, balance
Behaviors: withdraw, deposit
Software Objects
Writing software often involves creating a computational model
of real-world objects and processes.
balance:
deposit()
withdraw()
Bank Example - Cont’d
When the program runs there will be many
Instance #1
instances of the account class.
Each instance will have its own account number: 054
number and balance (object state)
Methods can only be invoked . balance: $19
Instance #2
number: 712
balance: $240
Instance #3
number: 036
balance: $941
Encapsulation
When classes are defined, programmers can specify that
certain methods or state variables remain hidden inside the
class.
Methods
accountNumber()
State variables make up the nucleus of the object. Methods surround and hide
(encapsulate) the state variables from the rest of the program.
Instance Methods and
Instance Variables
The methods and variables described in this module so far
are
know as instance methods and instance variables.
instanc
A class variable defines an attribute of an entire class.
In contrast, an instance variable defines an attributeeof
a single instance of a class. variable
s
Account
class
variable count: 3 num: 036
num: 054 num: 712
bal: $19 bal: $240 bal: $941
printCoun
Class t()
method
Inheritance
Examples:
Generic Class Terrier is
Terrier can be defined as a for Dog derived
subclass of Dog. With general
from Dog
class SavingsAccount
class Account { extends Account {
method acctNum() method rate() {…}
{…} }
method balance()
{…}
class
method deposit()
CheckingAccount
{…}
extends Account {
method withdraw()
method withdraw()
{…}
{…}
}
}
New Account Types - Cont’d
Account SavingsAccount CheckingAccount
rate() withdraw()
Manager Employee
Message
To: Employee
Method: getHired
Parameters: salary = $45,000, start_date =
10/21/99
Benefits of Messages