Chapter 10 Thinking in Objects Part 1
Class Abstraction & Encapsulation Class abstraction means to separate class implementation from the use of the class The creator of the class provides a description of the class and let the user know how the class can be used The user of the class does not need to know how the class is implemented. The detail of implementation is encapsulated and hidden from the user Example: The use of a PC does not require the knowledge of its internal workings
Example: The Loan Class A specific loan can be viewed as an object of a Loan class. The interest rate, amount, and period are its properties, and computing the monthly & total payments are its methods. When you buy a car, a loan object is created by instantiating the class with your loan interest rate, amount, and period. You can then use the methods to find the monthly & total payments. As a user of the Loan class, you don’t need to know how these methods are implemented Example: The Loan Class Loan TestLoanClass Run
Object-Oriented Thinking Chapters 1-8 introduced fundamental programming techniques for problem solving using loops, methods, and arrays. The studies of these techniques lay a solid foundation for object- oriented programming Classes provide more flexibility and modularity for building reusable software. This section improves the solution for a problem introduced in Chapter 3 using the object-oriented approach From the improvements, you will gain the insight on the differences between the procedural programming and object- oriented programming and see the benefits of developing reusable code using objects and classes
Example: The BMI Class BMI UseBMIClass In procedural programming, data & operations on the data are separate. It requires passing data to methods. The OO approach mirrors the real world, in which objects are associated with both data & operations. Using objects improves reusability, making programs easier to develop and maintain. An OO program can be viewed as a collection of cooperating objects. This example is an OO version of the procedural program of Listing 3.4 Example: The BMI Class BMI UseBMIClass Run
Class Relationships Association Aggregation Composition Inheritance Association is a general binary relationship that describes an activity between two classes
Aggregation Composition A "uses" B. B exists independently (conceptually) from A Example: A Company is an aggregation of People A Company is a composition of Accounts When a Company ceases to do business its Accounts cease to exist but its People continue to exist Composition A "owns" B. B has no meaning or purpose in the system without A Example: A Text Editor owns a Buffer (composition) A Text Editor uses a File (aggregation) When Text Editor is closes, the Buffer is destroyed but the File is not Source: programmers.stackexchange.com/questions/61376/aggregation-vs-composition/61527#61527
Aggregation & Composition Composition is actually a special case of the aggregation relationship Aggregation models has-a relationships and represents an ownership relationship between two objects The owner object is called an aggregating object and its class an aggregating class. The subject object is called an aggregated object and its class an aggregated class
Aggregation & Composition An aggregation relationship is represented as a data field in the aggregating class
Aggregation Between Same Class Aggregation may exist between objects of the same class public class Person { // The type for the data is the class itself private Person supervisor; ... }
Aggregation Between Same Class What happens if a person has several supervisors?
Example: The Course Class The Course class encapsulates the internal implementation. The user can create a Course object and manipulate it through the public methods addStudent, dropStudent, getNumOfStudents and getStudents. However, the user doesn’t need to know how these methods are implemented This example uses an array to store students, but you could use a different data structure to store students. The program that uses Course does not need to change as long as the contract of the public methods remains unchanged Course TestCourse Run
Source: https://www.youtube.com/watch?v=CgFVgp_VCN8 Stack in Motion Source: https://www.youtube.com/watch?v=CgFVgp_VCN8
Example: Stack of Integers
Stack of Integers
The StackOfIntegers Class TestStackOfIntegers Run