210 likes | 502 Views
Object-Oriented Programming Using Java. Introduction to OOP. Programming Paradigms. Imperative Programming ( C , Pascal, …) Functional Programming (Lisp, ML, …) Logic Programming (Prolog) Concurrent Programming (Ada, Occam, …) Object-oriented Programming (Smalltalk, C++, Java , …)
E N D
Object-Oriented Programming Using Java Introduction to OOP Michael Fung, CS&E, The Chinese University of HK
Programming Paradigms • Imperative Programming (C, Pascal, …) • Functional Programming (Lisp, ML, …) • Logic Programming (Prolog) • Concurrent Programming (Ada, Occam, …) • Object-oriented Programming(Smalltalk, C++, Java, …) • This is what we shall teach (through Java). Michael Fung, CS&E, The Chinese University of HK
The Object-Oriented (OO) Programming Paradigm • Object-oriented Programming is one of the programming paradigms (school of thought, methodology) in computer science. • Object-oriented Programming is well-known in the business world by the name of ‘Object Technology.’ • Synonyms: Object == Instance==EntityClass~=Static~=Type Michael Fung, CS&E, The Chinese University of HK
Why OOP? • Objects have two properties • fields (instance variables) and methods. • Fields tell you what an object is (properties). • Methods tell you what an object does (tasks). • Object oriented programming is alleged to have a number of advantages including: • Simpler, easier to read programs • More efficient reuse of code • Faster time to market • More robust, error-free code Michael Fung, CS&E, The Chinese University of HK
peterSaving bill michael Customer Customer Account Objects • Our world is full of objects. Graphical representation of objects Object name Object ‘type’ i.e. Class name Michael Fung, CS&E, The Chinese University of HK
bill peterSaving michael Customer Customer Account Objects • Our world is full of objects. Graphical representation of objects Michael Fung, CS&E, The Chinese University of HK
Modeling Our World • We try to model this object world. • Objects can accomplish tasks and keep data/state. • e.g.A drink dispensing machine sells Coke. It has a stock of 100 cans. • Inhuman?! • Certainly, but it helps us to program a computer in an organized and manageable manner. Michael Fung, CS&E, The Chinese University of HK
Verbs and Nouns • Verbs Action/ Task Method • Nouns Entity Field A drink dispenser can sell coke, sell lemon tea. Its stock includes number of coke, number of lemon tea. It can deliver change. • Any implicit (or missing) verb/ noun? • Can you suggest a class name? Michael Fung, CS&E, The Chinese University of HK
Customer bill michael Customer Customer Classes • A class (e.g., Customer) is a kind of mold or template to create objects (e.g., michael and bill). • An object is an instance of a class. The object belongs to that class. Object Class ‘instance-of’ Michael Fung, CS&E, The Chinese University of HK
billGates Person Person Account michael johnCheque Person Account More Class Examples peterSaving Account Michael Fung, CS&E, The Chinese University of HK
Object-Oriented Programming • We first defineclasses. • While the program is running, we may create objects from these classes. • We may store information in classes and objects. • We send messages to a class or an object to instruct it to perform a task. (For example, we send a deposit $250.00 message to an Account object to deposit $250.00 into the account.) Michael Fung, CS&E, The Chinese University of HK
Account johnSaving Account new Creating Objects • An object is created by sending a new message to a class. An instance (new object) is returned to the sender ‘instance-of’ Method main( ) Message sender Message sent to Class Account: new (ask it to create a new Account object) Michael Fung, CS&E, The Chinese University of HK
Account Account name Michael balance $123.45 deposit withdraw mySavingAC … Messages and Methods • An object consists of fields to store data and methods to manipulate the data. Argument of the message Fields $250.00 Methods Message: deposit $250.00 Michael Fung, CS&E, The Chinese University of HK
Account Account Account name Raymond name Michael balance $58.12 balance $123.45 deposit deposit withdraw withdraw … … Messages and Methods • A message may instruct an object to change the fields (state) of the object itself through a method. $250.00 myAccount yourAccount Michael Fung, CS&E, The Chinese University of HK
Account Account Account name Raymond name Michael balance $58.12 balance $373.45 deposit deposit withdraw withdraw … … Messages and Methods • A message may instruct an object to change the fields (state) of the object itself through a method. myAccount yourAccount Michael Fung, CS&E, The Chinese University of HK
Account Account name Michael balance $373.45 deposit withdraw … Messages and Methods • A method may return a value to the message sender. $999.00 myAccount Michael Fung, CS&E, The Chinese University of HK
Account Account name Michael Result “Failure” balance $373.45 deposit withdraw … Messages and Methods • An object may return a value to the message sender through the method. myAccount Michael Fung, CS&E, The Chinese University of HK
Account Account name Michael balance $373.45 deposit withdraw … Messages and Methods • Of course, we CANNOT send arbitrary messages to an arbitrary classes or objects. deposit $30.00 ? sell Coke rob $10,000.00 ? myAccount Michael Fung, CS&E, The Chinese University of HK
Summary • Object-Oriented Programming (OOP) treats entities as objects, modeled by classes. • Objects and classes can • store data in fields and • perform actions in methods. Michael Fung, CS&E, The Chinese University of HK
End Note • Readings and References • Preface • Chapter 1 Introduction • Exercise • 1.1 • Programming Projects • 1.1, 1.2 Michael Fung, CS&E, The Chinese University of HK