1.62k likes | 2.04k Views
Introduction to Object-Oriented Programming and the Java Programming Language. Session 1: Introduction to Object-Oriented Programming Objects, Messages And Classes. Session 1: Overview. O-O Roadmap Basic Concepts and Vocabulary (OOP-speak) Objects in an Executing Program
E N D
Introduction toObject-Oriented Programmingand theJava Programming Language
Session 1:Introduction toObject-Oriented ProgrammingObjects, Messages And Classes
Session 1: Overview • O-O Roadmap • Basic Concepts and Vocabulary (OOP-speak) • Objects in an Executing Program • Kindergarten Object-Oriented Design • Stock Portfolio Problem • Vending Machine Problem
Characteristics of OOP • Programming as modeling • The model elements are called objects • An executing program is a collection of objects
In Object-Oriented Programming... Objects are the active elements in the program. Not: “the computer does …” Rather: “the object does …”
Objects and Messages • Objects are activated by messages • Objects act by sending messages • Senders and receivers
What's In A Message? Name of Desired Behavior print (method name) Additional Information “Hello world” (arguments) print(“Hello world”)
What's In An Object? Code To Implement Various Behaviors (methods) push, pop, isEmpty withdraw, deposit, getBalance Internal State (variables) arr, top accountNum, owner, balance, dateOpened
argument1 argumentN method1 method2 method3 method4 Messages and Methods method1 An Object (sender) A Message: Method name + arguments An Object (receiver)
method1 method2 method2 method2 method3 message argument1 method4 argumentN An Object (sender) An Object (receiver) Returned object When An Object Recieves A Message
method1 argument1 … argumentN Behavior • Behavior: what an object does • Determined by object's methods • Instigated by receipt of message method1 method2 Object (receiver)
Prototype Syntactic Description of • Message • Method name • Arguments accompanying objects in the message • Message result • an object that is returned to the sender
Interface • Collection of prototypes • Interface of object: prototypes of the object's methods • (Java uses the term interface for a related but more language-specific concept)
Classes • Class— a repository of behavior • Object: an instance of a class • Object’s behavior defined by its class • Instances of the same class share behavior
State • Variables internal to object— instance variables • Modifies behavior of objects of the same class • Example: 2 Name objects w/ a getFirst method • Instances of the same class • Share behavior • Differ in state • Generally hidden, invisible
get first get first get first get first get first get first Jackie Jones Name get first add title Joe Thurm Jackie Joe Yudy Gerry Weiss Instances and State
Object Creation (I) • The Creation Problem: The object does not exist yet— can’t send it a message • Possible Solutions? • Implicit or explicit message to class (as if the class were an object!) • Special creator object • Not a real problem, just metaphysical!
Object Initialization Who should take responsibility for initializing an object’s state upon creation? THE OBJECT ITSELF OF COURSE!
Constructors • special methods of the object • invoked during creation only
SomeClass SomeClass method1 method1 methodN methodN constructor constructor New Object New Object Object Creation: Summary Creating Object constructor
Program Initiation • Similar to the Object Creation Problem • if no objects exist at the start, how does anything happen? • One Initiation Myth: • Special application object • External entity sends it a start message invoking a start method • Not a real problem, just metaphysical!
Imperative Programming— Gone? • Lives in modified form in method definitions • assignment • control structures • message-sending • non-object data types (language dependent)
Imperative ProgrammingUnder OOP: • The object, not the computer, is the actor • minor point perhaps? (I don’t think so!)
O-O vs Procedural Programming (I) • Procedural: • if you want something done find a procedure and call it • If no procedure exists, write one
O-O vs Procedural Programming (II) • OOP: • If you want something done find an object and send it a message • If no object exists, find a class and create an instance • If no class exists, write a class definition
O-O vs Procedural Programming (III) • Procedural programmer spends life writing procedures • O-O programmer spends life writing class definitions
Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of The Problem: Given a source of time card information and a personnel file, print payroll checks
What Objects Would You Expect? Statement of The Problem (again): Given a source of time card information and a personnel file, print payroll checks Common heuristic: look for the nouns.
PayrollMaster Employee PersonnelFile TimeCard TimeCardFile Check SS# Hours Payroll Classes
Time getHours Program PersonnelFile main lookup PayYourself Employee TimeCard File getCard Payroll Master TimeCard DoPayroll getTime SS Check getSS match print Payroll Objects
Program PersonnelFile PersonnelFile PersonnelFile main lookup lookup lookup TimeCard File TimeCard File TimeCard File getCard getCard getCard Payroll Master Payroll Master Payroll Master DoPayroll DoPayroll DoPayroll Payroll Program (1)
PersonnelFile lookup TimeCard TimeCard TimeCard TimeCard File getTime getTime getTime getSS getSS getSS getCard Payroll Master DoPayroll PayrollMaster: DoPayroll (1)
PersonnelFile lookup get SS get SS get SS get SS TimeCard TimeCard File getTime getSS getCard Payroll Master DoPayroll SS SS SS SS match match match match PayrollMaster: DoPayroll (2)
Time Time Time Time Time getHours getHours getHours getHours getHours PersonnelFile lookup get Time get Time get Time get Time TimeCard TimeCard File getTime getSS getCard Payroll Master DoPayroll SS match PayrollMaster: DoPayroll (3)
Time getHours PersonnelFile lookup PayYourself PayYourself PayYourself Employee Employee Employee TimeCard File getCard Payroll Master TimeCard DoPayroll getTime SS getSS match Personnel: Lookup
Time getHours PersonnelFile lookup PayYourself Employee TimeCard File getCard Payroll Master TimeCard DoPayroll getTime SS Check Check Check Check Check Check getSS match print print print print print print PayrollMaster: DoPayroll (4)
Time getHours Program PersonnelFile main lookup PayYourself Employee TimeCard File getCard Payroll Master TimeCard DoPayroll getTime SS Check getSS match print Payroll Program (2)
SMART and HELPFUL Objects A Peculiar Employee Design? • Since when do employees create their own checks? • Since when are employees trusted to determine their own pay? (not in OUR contract!)
BREAK! Coming Up: Object-Oriented Design
OOP and O-O Design • Can't rush in and “program” • Design phase needed even in simple problems • desirable in procedural programming • sine qua non in O-O world • O-O design methodologies: • numerous • we'll consider just one simple one
Statement of Problem Possible Objects Primary Object Behavior Interface Sample Use Implement Kindergarten OOD
Kindergarten OOD • Not appropriate for all problems • Works surprisingly well on many • great for CS1 • Illustrate with an example: Find the value of a portfolio of stocks based on “ticker-tape” information
Statement of Problem: Find the value of a portfolio of stocks based on "ticker-tape" information.
Possible Objects: • Portfolio • Holding (a portfolio item) • Value • TickerTape
Primary Object? • not Holding • not Value • Portfolio vs. TickerTape?
Portfolio vs. TickerTape? • Both primary in the sense of independence • But to which object should we send a message to solve our problem?
Is Portfolio the Primary Object? • If so, Portfolio has this behavior: get Value of Portfolio, given TickerTape
Is TickerTape the Primary Object? • If so, TickerTape has this behavior: • get Value of Portfolio, given Portfolio • Should TickerTape be responsible for computing the Value of a Portfolio?
Responsibility-Driven Design: • objects should be responsible for themselves • example: • objects should be responsible for their own initialization (constructors) • TickerTape should not be responsible for Value of a Portfolio, Portfolio should • Primary Object:Portfolio
Behavior of Portfolio: • get Value, given TickerTape • constructor, given a file listing of a portfolio