1 / 20

Object-Oriented Programming Using Java

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 , …)

jeanene
Download Presentation

Object-Oriented Programming Using Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Programming Using Java Introduction to OOP Michael Fung, CS&E, The Chinese University of HK

  2. 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

  3. 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

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

  5. 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

  6. 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

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

  8. 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

  9. 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

  10. billGates Person Person Account michael johnCheque Person Account More Class Examples peterSaving Account Michael Fung, CS&E, The Chinese University of HK

  11. 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

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

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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

More Related