Com S 342 meeting -*- Outline -*- * data-abstraction ** overview of this chapter key ideas: compound data -- means of combination data abstraction -- means of abstraction ** introduction to data abstraction (this assumes that the students have already had some experience with data abstraction) Q: what is data abstraction? Q: what are its advantages and disadvantages? Q: what are the properties that are achieved? representation independence, information hiding, separation of concerns Q: how is this done in C++? implementation | clients ^ \- "abstraction barrier" = interface (operations) ** example: rational numbers *** in Scheme "wishful thinking" suppose we had ways to make up and take apart the rational numbers **** constructor/observers (make-rat n d) (numer r) (denom r) **** specification we could ask that these satisfy the following equations: (numer (make-rat n d)) = n (denom (make-rat n d)) = d but that would be too strong, as make-rat could only make pairs, because for example, (numer (make-rat 2 4)) would be 2 instead we want a weaker specification (2.1.3) (numer (make-rat n d)) n _____________________ = _ (denom (make-rat n d)) d **** coding Scheme does not have a facility like Java classes for data abstraction Do add-rat, and equal-rat? Q: can we test this yet? have to implement make-rat,n umer, denom do this: -- using a pair explain about cons, car, and cdr (car (cons x y)) = x (cdr (cons x y)) = y -- as a reversed pair -- reduced to lowest terms talk about representation invariants Q: do we get all the advantages in Scheme? No, no information hiding is enforced opaque vs. transparent reps Java can do better ... ** in Java one could do it straightforward translation using static methods (and that has some advantages because of the binary methods), but we prefer a more object-oriented solution. *** a class with no static methods before the "wishful thinking" idea, write the data and equals methods above line that separates the client from representation code talk about the "this" keyword in Java Note that the equals method has to take an Object as an argument see class Rat *** separating representation and extensions **** inheriting from template parameters in C++, one could inherit from a template parameter, the Java does not have templates **** using factory methods and meta-classes the idea is to keep separation between the representation part and the extensions Talk about the need for interfaces for the "factory" methods, and the idea of meta-classes See the *IF files for the interfaces; the *Meta* files are for meta-classes and the *Rep* files are for the representations. Show how this allows the same extension to work for several different representations *** recap, summary Q: does Java give us the advantages of data abstraction? How?