Introduction to Object-Oriented Programming
Kaustubh Kulkarni
Object-Oriented Programming
1 - 1
What is an Object?
Real-world objects have states / attributes and behaviors.
Examples:
1 - 2
Software Objects
Writing software often involves creating a computational model
of real-world objects and processes.
1 - 3
Software Objects - Cont’d
In traditional programming languages (Fortran, Cobol, C, etc)
data structures and procedures are defined separately.
Account
Account
Account
balance:
number:
Bank
deposit()
withdraw()
1 - 4
Classes
The definitions of the attributes and methods of an object are
organized into a class. Thus, a class is the generic definition
for a set of similar objects (i.e. Person as a generic definition
for Jane, Mitch and Sue)
1 - 5
Classes - Cont’d
1 - 6
Bank Example
class: Account
deposit()
withdraw()
balance:
number:
1 - 7
Bank Example - Cont’d
balance: $240
number: 712
balance: $941
number: 036
balance: $19
number: 054
Instance #1
Instance #2
Instance #3
1 - 8
Encapsulation
When classes are defined, programmers can specify that
certain methods or state variables remain hidden inside the
class.
Hidden State Variables and Methods
Visible Methods
Visible Variables
Class Definition
1 - 9
Graphical Model of an Object
State variables make up the nucleus of the object. Methods surround and hide (encapsulate) the state variables from the rest of the program.
theBalance
acctNumber
accountNumber()
balance()
Instance variables
Methods
deposit()
withdraw()
1 - 10
Instance Methods and Instance Variables
The methods and variables described in this module so far are
know as instance methods and instance variables.
1 - 11
Class Methods and Class Variables
In addition to instance methods and instance variables, classes
can also define class methods and class variables.
1 - 12
Class Variables
count: 3
printCount()
num: 054 bal: $19
num: 712 bal: $240
num: 036 bal: $941
Account
Class method
class variable
instance variables
1 - 13
Inheritance
The advantage of making a new class a subclass is that it will
inherit attributes and methods of its parent class (also called
the superclass).
1 - 14
Subclasses
When a new class is developed a programmer can define it to
be a subclass of an existing class.
Examples:
Generic Class for Dog
With general attributes and behaviors for all dogs.
Specific Class for Terrier
With new attributes and behaviors specific to the Terrier breed.
Terrier is derived
from Dog
1 - 15
New Account Types - Cont’d
Suppose we define SavingsAccount and CheckingAccount
as two new subclasses of the Account class.
class Account {� method acctNum() {…} � method balance() {…}� method deposit() {…}� method withdraw() {…}�}
class SavingsAccount � extends Account {� method rate() {…} �}
class CheckingAccount � extends Account {� method withdraw() {…} �}
1 - 16
New Account Types - Cont’d
deposit()
acctNum()
balance()
withdraw()
deposit()
acctNum()
balance()
withdraw()
deposit()
acctNum()
balance()
withdraw()
rate()
withdraw()
Account
CheckingAccount
SavingsAccount
No new code has to be written for deposit() and other methods, they are inherited from the superclass.
1 - 17
Messages
Manager
Employee
Message�To: Employee�Method: getHired�Parameters: salary = $45,000, start_date = 10/21/99
1 - 18
Benefits of Messages
Message passing supports all possible interactions between
two objects.
1 - 19
Polymorphism
Polymorphism is one of the essential features of an object-
oriented language; this is the mechanism of decoupling the
behavior from the message.
1 - 20
Polymorphism – Cont’d
There are many forms of Polymorphism in object-oriented
languages, such as:
1 - 21
OO Concepts Summary
1 - 22