Top Banner
Object Oriented Object Oriented Programming Programming CSC 171 FALL 2001 LECTURE 11
26
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Object Oriented ProgrammingObject Oriented Programming

CSC 171 FALL 2001

LECTURE 11

Page 2: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

History: ENIAC History: ENIAC EElectronic lectronic NNumerical umerical IIntegrator ntegrator aand nd

CComputeromputer 1943 – 1947 Work on

ENIAC at the Univ. of Pennsylvania

John Mauchly &

J. Presper Eckert.

The world's first electronic digital computer was developed to compute World War II ballistic firing tables.

Page 3: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Computer ScienceComputer Science

What is computer science?

Page 4: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Computer ScienceComputer Science

“fundamentally, computer science is the science of abstraction – creating the right model for a problem and devising the appropriate mechanizable technique to solve it.”

- A. Aho and J. Ullman

Page 5: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Acts of mind - LockeActs of mind - Locke

“The acts of the mind, wherein it exerts its power over simple ideas, are chiefly these three:

Page 6: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

AggregationAggregation

1. Combining several simple ideas into one compound one, and thus all complex ideas are made

Page 7: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

ComparisionComparision

2. The second is brining two ideas, where simple or complex, together, and setting them by one another so as to take aview of them at once,without uniting them into one, by which it gets all its ideas of relations.

Page 8: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

AbstractionAbstraction

3. The third is separating them from all other ideas that accompany them in their real existence: this is called abstraction, and thus all general ideas are made.”

John Locke, An Essay Concerning

Human Understanding, (1690)

Page 9: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Object OrientationObject Orientation

Objects are the means of aggregation and

abstraction,

in an OO programming language

Aggregation – bundling data elementsAbstraction - inheritance

Page 10: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Data EncapulationData Encapulation

OOP encapulates– data – behavior

Page 11: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

InterfacesInterfaces

We define systems in terms of many objects– Each type of object has certain behaviors

methods

– Each object has certain data Instance variables

Objects communicate with other viawell defined interfaces

Page 12: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Information HidingInformation Hiding

When we design objects which communicate exclusively via interfaces - we insulate (abstract) the outward functionality

from the inward implementation.

This insulation is the principle of information hiding – the inward details of the implementation are hidden from the outside objects

Page 13: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Benefits of Information HidingBenefits of Information Hiding

Information hiding promotes program modifiability.

Clients are not required to know the internals of a class in order to use it.

So, if the class changes inside, then the client need not be changed

The client and the object are said to be “loosely coupled”

Page 14: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Example – A bank accountExample – A bank account

Page 15: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

A specific instanceA specific instance

Page 16: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Multiple instancesMultiple instances

Page 17: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Bank Account - behaviorsBank Account - behaviorspublic class BankAccount{

private double balance;

public BankAccount() { balance = 0; }

public BankAccount(double initialBalance){ balance = initialBalance;

}

public void deposit(double amount){ balance = balance + amount; }

public void withdraw(double amount){ balance = balance - amount; }

public double getBalance(){return balance;}}

Page 18: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

thisthis

Java conserves storage by maintaining only one copy of each method per class

The same method is invoked by every object Every object has its own copy of its instance

variables Every object, by default has a reference to itself

– “this” is the name of every object’s reference to itself

Page 19: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Bank Account - thisBank Account - thispublic class BankAccount{

private double balance;

public BankAccount() { this.balance = 0; }

public BankAccount(double balance){ this.balance = balance;

}

public void deposit(double amount){ this.balance = this.balance + amount; }

public void withdraw(double amount){ this.balance = this.balance - amount; }

public double getBalance(){return this.balance;}

}

Page 20: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Sometimes, classes shareSometimes, classes share

Consider a variation of the BankAccount

public class BankAccount { . . . . private double balance; private int accountNumber; // we want to assign sequential numbers }

Page 21: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Sharing data between objects Sharing data between objects of the same classof the same class

We want to set the account number automaticallypublic class BankAccount { private double balance; private int accountNumber; private int lastAssignedNumber = 0; //Will this work?public BankAccount() {

lastAssignedNumber++;accountNumber = lastAssignedNumber; //???????

}

Page 22: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Static/class variablesStatic/class variables

We don’t want each instance of the class to have its own value for lastAssignedNumber

We need to have a single value that is the same for the entire class.

These are called class or static variables

Page 23: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Sharing data between objects Sharing data between objects of the same classof the same class

We want to set the account number automaticallypublic class BankAccount { private double balance; private int accountNumber; private static int lastAssignedNumber = 0; public BankAccount() {

lastAssignedNumber++;accountNumber = lastAssignedNumber;

}

Page 24: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.
Page 25: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

FinallyFinally

Some types of variables are fixed constants, that we do not want to change

Like conversion factors We can use the keyword “final” to prevent

changes So we have a “constant variable” Sort of like

– “jumbo shrimp”– “freezer burn”

Page 26: Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Classes share constantsClasses share constants

Consider a variation of the BankAccount

public class Converter {

private final static double miles2km = 0.6;

}