This example demonstrates the advantages of the object oriented paradigm over

This example demonstrates the advantages of the

This preview shows page 13 - 15 out of 33 pages.

This example demonstrates the advantages of the object-oriented paradigm over the proce- dural paradigm. The procedural paradigm focuses on designing methods. The object-oriented paradigm couples data and methods together into objects. Software design using the object- oriented paradigm focuses on objects and operations on objects. The object-oriented approach public String getStatus() { public double getBMI() { this (name, 20 , weight, height); constructor constructor getBMI getStatus procedural vs. object-oriented paradigms
Image of page 13
382 Chapter 10 Thinking in Objects combines the power of the procedural paradigm with an added dimension that integrates data with operations into objects. In procedural programming, data and operations on the data are separate, and this methodology requires sending data to methods. Object-oriented programming places data and the operations that pertain to them in an object. This approach solves many of the problems inherent in procedural programming. The object-oriented programming approach organizes programs in a way that mirrors the real world, in which all objects are associated with both attributes and activities. Using objects improves software reusability and makes programs easier to develop and easier to maintain. Programming in Java involves thinking in terms of objects; a Java program can be viewed as a collection of cooperating objects. 10.9 Is the BMI class defined in Listing 10.4 immutable? 10.7 Object Composition An object can contain another object. The relationship between the two is called composition . In Listing 10.2, you defined the Loan class to contain a Date data field. The relationship between Loan and Date is composition. In Listing 10.4, you defined the BMI class to contain a String data field. The relationship between BMI and String is composition. Composition is actually a special case of the aggregation relationship. Aggregation modelshas-arelationships and represents an ownership relationship between two objects. The ownerobject is called an aggregating objectand its class an aggregating class. The subject object iscalled an aggregated objectand its class an aggregated classAn object may be owned by several other aggregating objects. If an object is exclu-sively owned by an aggregating object, the relationship between them is referred to ascomposition. For example, “a student has a name” is a composition relationship betweentheStudentclass and the Nameclass, whereas “a student has an address” is an aggrega-tion relationship between the Studentclass and the Addressclass, because an addressmay be shared by several students. In UML notation, a filled diamond is attached to anaggregating class (e.g., Student) to denote the composition relationship with an aggregatedclass (e.g., Name), and an empty diamond is attached to an aggregating class (e.g., Studentto denote the aggregation relationship with an aggregated class (e.g., Address), as shownin Figure 10.6. . )
Image of page 14
Image of page 15

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture