© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Chapter 1 Introduction to Object-oriented Programming and Software Development
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Chapter 1 Objectives After you have read and studied this chapter, you should be able to Name the basic components of object-oriented programming. Differentiate classes and objects. Differentiate class and instance methods. Differentiate class and instance data values. Draw object diagrams using icons for classes, objects, and other components of object-oriented programming. Describe the significance of inheritance in object- oriented programs.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Classes and Objects Object-oriented programs use objects. An object is a thing, both tangible and intangible. Account, Vehicle, Employee, etc. To create an object inside the computer program, we must provide a definition for objects—how they behave and what kinds of information they maintain —called a class. An object is called an instance of a class.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Graphical Representation of an Object Account SV129 The class name is placed inside the object icon. The object’s name appears on top of the icon. An icon for an object is the rounded rectangle. The class name may be omitted when it is clear from the context which class the object belongs to. customer1
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Graphical Representation of a Class Account The class name appears on top of the icon. An icon for a class is the rectangle.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Instance-of Relationship Employee Bill Employee Steve Employee Andy The class name can be omitted since it is clear which class these objects belong to. The dotted line shows the instance-of relationship. Before you can create instances of a class, the class must be defined.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Messages and Methods To instruct a class or an object to perform a task, we send a message to it. You can send a message only to the classes and objects that understand the message you sent to them. A class or an object must possess a matching method to be able to handle the received message. A method defined for a class is called a class method, and a method defined for an object is called an instance method. A value we pass to an object when sending a message is called an argument of the message.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Sending a Message Message deposit with the argument is sent to chk-008. Account chk-008 deposit deposit Message name is usually omitted in the diagram. deposit
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Sending a Message and Getting an Answer This message has no argument. Account chk-008 getMonthlyFee monthly fee The method returns the value monthly fee back to the message sender.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Calling a Class Method Account getAverageBalance average balance The average balance of all accounts is returned.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Summary of Class and Object Icons Squared corners are used for a class and class methods. An instance method icon is drawn in a dotted line. Rounded corners are used for an object and instance methods.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Class and Instance Data Values An object is comprised of data values and methods. An instance data value is used to maintain information specific to individual instances. For example, each Account object maintains its balance. A class data value is used to maintain information shared by all instances or aggregate information about the instances. For example, minimum balance is the information shared by all Account objects, whereas the average balance of all Account objects is an aggregate information.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Sample Instance Data Value Account SV129 Account SV506 Account SV008 current balance All three Account objects possess the same instance data value current balance. The actual dollar amounts are, of course, different
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Sample Class Data Value Account SV129 Account SV506 Account SV008 current balance Account minimum balance There is one copy of minimum balance for the whole class and shared by all instances.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Variable and Constant Data Values There are two types of data values: Account SV129 minimum balance current balance account prefix 6427 opening balance A constant whose value must remain fixed over time. A variable whose value can change over time.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Inheritance In object-oriented programming, we use a mechanism called inheritance to design two or more entities that are different but share many common features. First we define a class that contains the common features of the entities. Then we define classes as an extension of the common class inheriting everything from the common class. We call the common class the superclass and all classes that inherit from it subclasses. We also call the superclass an ancestor and the subclass a descendant.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Account and Its Subclasses Account minimum balance Savings Checking minimum balance This class becomes the superclass of two subclasses… when (sub)classes inherit from it. Inherited components are not shown in the subclasses unless… the subclass(es) overrides them.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Sample Inheritance Hierarchy Account Savings Checking SuperSaver RegularStudent Interest Bearing ATM Checking
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Having Fun with Java /* Program FunTime The program will allow you to draw a picture by dragging a mouse (move the mouse while holding the left mouse button down; hold the button on Mac). To erase the picture and start over, click the right mouse button (command-click on Mac). */ import javabook.*; class FunTime { public static void main(String[ ] args) { SketchPad doodleBoard; doodleBoard = new SketchPad(); doodleBoard.setVisible( true ); } Declare a name Create an object Make it visible
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Object Diagram for FunTime FunTime main SketchPad doodleBoard setVisible true
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Execution Flow of the FunTime Program SketchPad doodleBoard; doodleBoard = new SketchPad(); doodleBoard.setVisible( true ); SketchPad doodleBoard; doodleBoard doodleBoard = new SketchPad(); SketchPad doodleBoard.setVisible( true );