210 likes | 308 Views
Chapter 1. Introduction to Object-oriented Programming and Software Development. 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.
E N D
Chapter 1 Introduction to Object-oriented Programming and Software Development Intro to OOP with Java--Wu
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. Intro to OOP with Java--Wu
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. Intro to OOP with Java--Wu
The object’s name appears on top of the icon. An icon for an object is the rounded rectangle. The class name is placed inside the object icon. customer1 The class name may be omitted when it is clear from the context which class the object belongs to. Graphical Representation of an Object SV129 Account Intro to OOP with Java--Wu
The class name appears on top of the icon. An icon for a class is the rectangle. Graphical Representation of a Class Account Intro to OOP with Java--Wu
Employee Before you can create instances of a class, the class must be defined. The dotted line shows the instance-of relationship. Steve Bill Andy The class name can be omitted since it is clear which class these objects belong to . Employee Employee Employee Instance-of Relationship Intro to OOP with Java--Wu
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. Intro to OOP with Java--Wu
Message deposit with the argument 250.00 is sent to chk-008. chk-008 Account deposit 250.00 deposit Message name is usually omitted in the diagram. 250.00 deposit Sending a Message Intro to OOP with Java--Wu
This message has no argument. chk-008 Account getMonthlyFee monthly fee The method returns the value monthly fee back to the message sender. Sending a Message and Getting an Answer Intro to OOP with Java--Wu
Account getAverageBalance average balance The average balance of all accounts is returned. Calling a Class Method Intro to OOP with Java--Wu
Squared corners are used for a class and class methods. <Class Name> <result> <result> An instance method icon is drawn in a dotted line. <Object Name> <Class Name> Rounded corners are used for an object and instance methods. Summary of Class and Object Icons Intro to OOP with Java--Wu
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. Intro to OOP with Java--Wu
SV506 SV129 SV008 Account Account Account current balance current balance current balance 1304.98 908.55 354.00 Sample Instance Data Value All three Account objects possess the same instance data value current balance. The actual dollar amounts are, of course, different. Intro to OOP with Java--Wu
minimum balance There is one copy of minimum balance for the whole class and shared by all instances. SV506 SV129 SV008 100.00 Account Account Account current balance current balance current balance 1304.98 908.55 354.00 Sample Class Data Value Account Intro to OOP with Java--Wu
minimum balance account prefix 100.00 6427 Account SV129 Account current balance opening balance 908.55 100.00 Variable and Constant Data Values There are two types of data values: A variable whose value can change over time. A constant whose value must remain fixed over time. Intro to OOP with Java--Wu
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. Intro to OOP with Java--Wu
This class becomes the superclass of two subclasses… minimum balance minimum balance 100.00 250.00 when (sub)classes inherit from it. the subclass(es) overrides them. Savings Checking Account and Its Subclasses Account Inherited components are not shown in the subclasses unless… Intro to OOP with Java--Wu
Account Savings Checking Regular Student Interest Bearing ATMChecking SuperSaver Sample Inheritance Hierarchy Intro to OOP with Java--Wu
Declare a name Create an object Make it visible 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 ); } } Intro to OOP with Java--Wu
doodleBoard FunTime SketchPad true setVisible main Object Diagram for FunTime Intro to OOP with Java--Wu
doodleBoard Execution Flow of the FunTime Program SketchPad doodleBoard; doodleBoard = new SketchPad(); doodleBoard.setVisible( true ); SketchPad doodleBoard; doodleBoard = new SketchPad(); doodleBoard.setVisible( true ); SketchPad Intro to OOP with Java--Wu