Intro to Object-Oriented Programming

Download Report

Transcript Intro to Object-Oriented Programming

Introduction to
Object-Oriented Programming
Department of Information Systems
and Computer Science
Ateneo de Manila University
Object-Oriented Programming



The traditional definition of a program is:
a sequence of instructions to be executed
on a computer
In the Object-Oriented Programming (OOP)
paradigm, a program that executes is a collection
of interacting objects
In this paradigm, the programs we write specify
what are in these objects and how these objects
behave
6/22/2005
Copyright 2005, by the authors of these slides, and Ateneo de
Manila University. All rights reserved
L3: Intro to OOP
Slide 2
So … What is an Object?

A “thing” that has type, identity, state, and
behavior



type: belongs to a “class” of similar objects
identity: is a distinct “instance” of a class of objects
state / attributes: has a set of properties (aka fields)


behavior: has “methods” (things that the object
knows how to do)

6/22/2005
each field can have different values
we say we “call” a method on the object
Copyright 2005, by the authors of these slides, and Ateneo de
Manila University. All rights reserved
L3: Intro to OOP
Slide 3
Examples of Objects




on (true or false)

behavior


LightBulb

state/attributes


switch on
switch off
check if on
state/attributes

behavior


Car



state/attributes


behavior

BankAccount
balance


deposit
withdraw
check balance

drive
load gas
change efficiency
check gas
check odometer reading
Note

each object is an “instance”
of that “type” of object

each instance has its own
values for its attributes

6/22/2005
# of liters of gas in tank
total # of km run so far
efficiency (km/liter)
e.g., different accounts can
have different balances
Copyright 2005, by the authors of these slides, and Ateneo de
Manila University. All rights reserved
L3: Intro to OOP
Slide 4
BankAccount example
(A Preview)

state/attributes


behavior

BankAccount
balance


public class BankAccount
{
private int balance = 0;
get balance
deposit
withdraw
public int getBalance()
{
return balance;
}
public void deposit( int amount )
{
balance = balance + amount;
}
…
BankAccount
int balance
int getBalance()
void deposit( int amount )
… and more
6/22/2005
BankAccount.java
}
Copyright 2005, by the authors of these slides, and Ateneo de
Manila University. All rights reserved
L3: Intro to OOP
Slide 5
Class Definition in Java
BankAccount
public class BankAccount
{
private int balance = 0;

type (or class)

state/attributes (fields)

behavior (methods)



may have input parameters
in parenthesis
may have output
(or “return”) type
has “body” with code
6/22/2005
BankAccount.java
public int getBalance()
{
return balance;
}
public void deposit( int amount )
{
balance = balance + amount;
}
…
}
Copyright 2005, by the authors of these slides, and Ateneo de
Manila University. All rights reserved
L3: Intro to OOP
Slide 6
A Class with a Constructor



Constructor: special
method that handles
initialization
For now, view
constructors as an
alternative to initializing
fields as they are
declared
Later: more advanced
uses for constructors
public class BankAccount
{
private int balance;
BankAccount.java
public BankAccount()
{
balance = 0;
}
public int getBalance()
{
return balance;
}
public void deposit( int amount )
{
balance = balance + amount;
}
…
}
6/22/2005
Copyright 2005, by the authors of these slides, and Ateneo de
Manila University. All rights reserved
L3: Intro to OOP
Slide 7
A Class and Its Instances

A single class can have multiple instances




Each instance is a separate object
Each instance can have different values for its
fields
The definition of methods is the same for all
instances of the same type
Thus, there is only one class definition

6/22/2005
Written as the .java file for that class
Copyright 2005, by the authors of these slides, and Ateneo de
Manila University. All rights reserved
L3: Intro to OOP
Slide 8
Lab Exercise: Try it in BlueJ


Create and compile a BankAccount class
(BankAccount.java)
Create BankAccount objects
(instances of the class)


Right-click on the class
Carry out operations on the BankAccount objects
(invoke the deposit and getBalance methods)

6/22/2005
Right-click on the instances
Copyright 2005, by the authors of these slides, and Ateneo de
Manila University. All rights reserved
L3: Intro to OOP
Slide 9
Lab Exercise, continued


Instantiate and use a BankAccount object
in a Java application
(use the command prompt)
How? In the same folder containing the
BankAccount class, create, compile and
execute BankSystem.java

6/22/2005
Inside the public static void main(String args[])
method, type the following code…
Copyright 2005, by the authors of these slides, and Ateneo de
Manila University. All rights reserved
L3: Intro to OOP
Slide 10
Lab Exercise, continued
BankAccount b = new BankAccount();
System.out.println( "depositing 1000..." );
b.deposit( 1000 );
System.out.print( "Balance is now: " );
System.out.println( b.getBalance() );
System.out.println( "depositing 500..." );
b.deposit( 500 );
System.out.print( "Balance is now: " );
System.out.println( b.getBalance() );
6/22/2005
Copyright 2005, by the authors of these slides, and Ateneo de
Manila University. All rights reserved
method
invocation
L3: Intro to OOP
Slide 11
Summary





In Java, we write programs for objects
These programs are called classes
A class consists of fields and methods to specify
the state and behavior for its objects
Once a class has been defined, objects of that
class can be created (instantiated)
Methods are invoked on an object, and may
cause the state of the object to change
6/22/2005
Copyright 2005, by the authors of these slides, and Ateneo de
Manila University. All rights reserved
L3: Intro to OOP
Slide 12