Object Oriented Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Object Oriented Programming

Description:

Electronic Numerical Integrator and Computer ... 'jumbo shrimp' 'freezer burn' Classes share constants. Consider a variation of the BankAccount ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 27
Provided by: thaddeusf
Category:

less

Transcript and Presenter's Notes

Title: Object Oriented Programming


1
Object Oriented Programming
  • CSC 171 FALL 2001
  • LECTURE 11

2
History ENIAC Electronic Numerical Integrator
and Computer
  • 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.

3
Computer Science
  • What is computer science?

4
Computer 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

5
Acts of mind - Locke
  • The acts of the mind, wherein it exerts its
    power over simple ideas, are chiefly these three

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

7
Comparision
  • 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.

8
Abstraction
  • 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)

9
Object Orientation
  • Objects are the means of
  • aggregation and
  • abstraction,
  • in an OO programming language
  • Aggregation bundling data elements
  • Abstraction - inheritance

10
Data Encapulation
  • OOP encapulates
  • data
  • behavior

11
Interfaces
  • 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 via
  • well defined interfaces

12
Information 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

13
Benefits 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

14
Example A bank account
15
A specific instance
16
Multiple instances
17
Bank Account - behaviors
  • public 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

18
this
  • 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 objects reference to
    itself

19
Bank Account - this
  • public 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

20
Sometimes, classes share
  • Consider a variation of the BankAccount
  • public class BankAccount
  • . . . .
  • private double balance
  • private int accountNumber
  • // we want to assign sequential numbers

21
Sharing data between objects of the same class
  • We want to set the account number automatically
  • public class BankAccount
  • private double balance
  • private int accountNumber
  • private int lastAssignedNumber 0 //Will
    this work?
  • public BankAccount()
  • lastAssignedNumber
  • accountNumber lastAssignedNumber //???????

22
Static/class variables
  • We dont 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

23
Sharing data between objects of the same class
  • We want to set the account number automatically
  • public class BankAccount
  • private double balance
  • private int accountNumber
  • private static int lastAssignedNumber 0
  • public BankAccount()
  • lastAssignedNumber
  • accountNumber lastAssignedNumber

24
(No Transcript)
25
Finally
  • 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

26
Classes share constants
  • Consider a variation of the BankAccount
  • public class Converter
  • private final static double miles2km 0.6
Write a Comment
User Comments (0)
About PowerShow.com