Lecture 16 Administrative: - midterm is returned --- Object Oriented Programming (I) History 1960s: Simula project: computer simulations of real-world situations -- a collection of objects, each of which has certain properties and has the ability to act/react in certain ways. => Simula67 1970s: abstract data types and module types 1980s: OO paradigm OO Languages: Simula, Simula67, Smalltalk, Eiffel, C++, Ada95, Modula-3, Java, CLOS, ... Basic terms - Object: container of data - Attribute value: specific property of object - Class: Formal definition of an object's attrubutes - Object: is an instance of a class - Method: Function acting on attributes -- "message to object" - Interface: External representation of Method - Implementation: internal representation of method - Subclass: child class, derived class that inherits from super class and maintains the superclass's interface. Major features of Object-Oriented programming - code reuse - *encapsulation and data hiding - *inheritance - polymorphism - *dynamic method dispatch * three fundamental concepts of OO programming Encapsulation and data hiding - can also be achieved through ADT and modules > much of the advantages of abstraction is however lost because the programmer must copy the pre-existing code, figure out how it works inside, and modify it by hand,rather than using it "as-is" - In OO, data hiding mechanism > private > protected > public overcome the problems with ADT and modules by introducing a better mechanism of code reuse -- inheritance. Inheritance - "is-a" versus "has-a" - extension - refinements (or redefinition) of existing abstractions - single, multiple, and "mix-in" inheritance class hierarchies ClosedFigure / \ / \ Polygon Ellipse / \ | / \ | Triangle Rect Circle | | Square A / \ / \ B C \ / \ / D A A ^ ^ | | | | B C \ / \ / D Pragmatics with inheritance: - how to allocate space for objects For example, class Point { int x = 0, y = 0; public void setVertical (int steps) { y += steps; } public void printType (void) { print ("point"); } } Point |---------| | x = 0 | |---------| | y = 0 | |---------| |f=ox8001 | |---------| allocate space for x and y sequentially in memory o offset to access x and y is fixed and known at compile time o why contiguous chunk of space is required (memory fragmentation is bad) class ColoredPoint extends Point { int y = 2; color = 7; public void printTYpe (void) { print ("colored point"); } } CPoint |---------| | x = 0 | |---------| | y = 0 | |---------| |color=2 | |---------| |f=ox8001 | |---------| or CPoint |---------| | x = 0 | |---------| | y = 0 | |---------| |f=ox8001 | |---------| | color=2 | |---------| An object of a subclass can be allocated as an extention of the preceding data object. As such, the instance variables x and y inherited by any ColoredPoint object from Point can be found at the same offset from the beginning of its allocated space as for any Point object, i.e., the location of x and y can be determined statically, even without knowing the exact class of an object. Further, space can be saved if an object does not carry pointers to all instance methods. Point |---------| Vtable | vtable |-----> |--------| |---------| | setX |----> | x = 0 | |--------| |---------| | setY |----> | y = 0 | |--------| |---------| | print |----> code for print |--------| CPoint |---------| Vtable | vtable |-----> |--------| |---------| | setX |---> code for setX | x = 0 | |--------| |---------| | setY |---> code for setY | y = 0 | |--------| |---------| | print |---------> code for redefined print |color= 2 | |--------| |---------| |setcolor|---------> code for setcolor |--------| Point cp = new ColoredPoint(); cp.printType(); - static method binding o dependent on reference -> "point" - dynamic method binding o dependent on object -> "colored point";