Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Post on 20-Dec-2015

222 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

Transcript

Object Oriented ProgrammingObject 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.

Computer ScienceComputer Science

What is computer science?

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

Acts of mind - LockeActs of mind - Locke

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

AggregationAggregation

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

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.

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)

Object OrientationObject Orientation

Objects are the means of aggregation and

abstraction,

in an OO programming language

Aggregation – bundling data elementsAbstraction - inheritance

Data EncapulationData Encapulation

OOP encapulates– data – behavior

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

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

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”

Example – A bank accountExample – A bank account

A specific instanceA specific instance

Multiple instancesMultiple instances

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;}}

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

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;}

}

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 }

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; //???????

}

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

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;

}

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”

Classes share constantsClasses share constants

Consider a variation of the BankAccount

public class Converter {

private final static double miles2km = 0.6;

}

top related