Download presentation
Presentation is loading. Please wait.
Published byCathleen Pearson Modified over 9 years ago
1
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK1 Object-Oriented Programming Using Java Introduction to OOP
2
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK2 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).
3
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK3 The Object-Oriented (OO) Programming Paradigm Object-oriented Programming is one of the programming paradigms (school of thought, methodology) in computer science. Object Technology Object-oriented Programming is well-known in the business world by the name of ‘Object Technology.’ Synonyms:Object == Instance == Entity Class ~= Static ~= Type
4
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK4 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
5
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK5 Customer bill Customer michael Account peterSaving Objects Our world is full of objects. Graphical representation of objects Object ‘type’ i.e. Class name Object name
6
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK6 Our world is full of objects. Graphical representation of objects Customer bill Objects Customer michael Account peterSaving
7
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK7 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.
8
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK8 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?
9
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK9 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. Customer bill Customer michael Customer ‘instance-of’ Class Object
10
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK10 More Class Examples Person michael Person billGates Person Account johnCheque Account peterSaving Account
11
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK11 Object-Oriented Programming We first define classes. 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.)
12
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK12 Account new Creating Objects An object is created by sending a new message to a class. Method Message sent to Class Account: new (ask it to create a new Account object) Message sender An instance (new object) is returned to the sender main( ) Account johnSaving ‘instance-of’
13
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK13 Messages and Methods An object consists of fields to store data and methods to manipulate the data. Account mySavingAC name Michael deposit withdraw … balance $123.45 Fields Methods Message: deposit $250.00 Argument of the message $250.00
14
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK14 Messages and Methods A message may instruct an object to change the fields (state) of the object itself through a method. Account myAccount Account name Michael deposit withdraw … balance $123.45 yourAccount Account name Raymond deposit withdraw … balance $58.12 $250.00
15
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK15 Messages and Methods A message may instruct an object to change the fields (state) of the object itself through a method. Account myAccount Account name Michael deposit withdraw … balance $373.45 yourAccount Account name Raymond deposit withdraw … balance $58.12
16
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK16 Messages and Methods A method may return a value to the message sender. Account myAccount Account name Michael deposit withdraw … balance $373.45 $999.00
17
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK17 Messages and Methods An object may return a value to the message sender through the method. Account myAccount Account name Michael deposit withdraw … balance $373.45 Result “ Failure ” Result “ Failure ”
18
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK18 Messages and Methods Of course, we CANNOT send arbitrary messages to an arbitrary classes or objects. Account myAccount Account name Michael deposit withdraw … balance $373.45 rob $10,000.00 sell Coke deposit $30.00
19
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK19 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.
20
2008-2009 1cMichael Fung, CS&E, The Chinese University of HK20 End Note Readings and References –Preface –Chapter 1 Introduction Exercise –1.1 Programming Projects –1.1, 1.2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.