Unformatted text preview:

Data Abstraction and OO Textbook Chapter 9 incl CD material 1 Data Abstraction and OO z Modules or Objects Classes Reduces conceptual load by modularizing code z Contains faults to small parts of code z Makes program components more independent z 2 Why OO z Groups data with code Data is isolated in private object fields Methods operating on the data are bundled with the data in classes Other code can t mess with the data Classes can be easily added or removed from the code 3 CSC 4101 Programming Languages 1 Programming Style z C class C public virtual int foo class D public C virtual int foo class E public C virtual int foo z C int foo C obj if obj is a D code from D foo else if obj is an E code from E foo else code from C foo 4 OO vs ADT Functional Style z Object oriented programming Extending a data structure is easy Adding new code to an existing data structure is hard z ADT Functional Imperative Style Adding new code is easy Extending a data structure is hard 5 ObjectObject Oriented Programming z User defined types z Encapsulation z Subtype polymorphism Classes Private fields Inheritance Structural subtyping z Code reuse Inheritance 6 CSC 4101 Programming Languages 2 What is a Type z Built in types int char etc Class types z Interface types in Java z z Think of a type as a set of values int 2 31 2 31 1 C set of instances of class C or any subclass of C 7 Subtyping C is a subtype of B C B or a C object isa B object z or a C object can be used wherever a B object is expected z or C is more specific than B z z z The set of C instances is a subset of the set of B instances 8 Implementation of Subtyping z The layout of a C object includes a B object p B p new C B C z If virtual methods are overridden we need to select the appropriate method at run time 9 CSC 4101 Programming Languages 3 Selection of Virtual Methods class B public int foo return 0 class C extends B public int foo return 1 B obj new C int i obj foo executes C foo 10 Method Selection Algorithm z Method call C p new D p foo 42 p z Compile time overload resolution Find the receiver s p s static type C Inside C find an appropriate method for the static types of the arguments int C z Run time virtual method dispatch Look up the code for the method signature foo int C in the virtual function table 11 Implementation of Method Dispatch z Conceptually an object contains a list of pointers to its methods z C the object contains a pointer to the virtual function table z Java an object pointer consists of a pointer to the object and a pointer to the dispatch table CSC 4101 Programming Languages 12 4 Other Design Issues z Single or multiple inheritance C class C public A public B Java class C extends B z C style virtual inheritance class C public virtual A allows objects to share a common part from multiple base classes 13 Other Design Issues z Single dispatch or multimethods single dispatch virtual functions multiple dispatch run time overloading z Function call or message passing C Java o foo Smalltalk o foo Typed or untyped z Interface types vs abstract classes z 14 Multiple Inheritance class A class B int y public virtual class C int z public virtual virtual int x int foo int public A public B int foo int void bar int 15 CSC 4101 Programming Languages 5 Implementation of Multiple Inheritance z z Concatenate all the pieces in layout Multiple vtables per object C X y B vptr C foo z C vptr C bar 16 Implementation of Multiple Inheritance z Adjust this pointer B p new C int i p foo 42 z is translated to int i p B vptr 1 fn p p B vptr 1 delta 42 17 Virtual Inheritance z Sharing of common part class class class class z A B public virtual A C public virtual A D public B public C A B C D Implementation B and C parts contain pointers to common A part 18 CSC 4101 Programming Languages 6 Multimethods Instead of run time method selection based on receiver virtual in C z Run time method selection based on all arguments z Like overloading but with method selection at run time z Allows different program structure z Languages CLOS Cecil Brew z 19 Multimethod Implementation z Generic functions dispatch using an n dimensional table lookup z Example int int C p int foo C foo D new D i foo p calls foo D 20 Function Call vs Message Passing z Function call syntax C p foo 5 p foo 5 z Message passing syntax SmallTalk p foo 5 6 7 someArray at 1 put 5 x 0 ifTrue y 1 ifFalse y 2 21 CSC 4101 Programming Languages 7 Typed vs Untyped z Statically typed C Java etc no message not understood errors at run time more efficient dispatch since layout of dispatch table is known z Untyped SmallTalk CLOS Cecil more flexibility try to infer types for optimizing dispatch 22 Abstract Classes class A public virtual int foo 0 class C public A z z Defines type Implementation is supplied in subclass 23 Interface Types interface I int foo class C implements I Separation of interface and implementation z Cleaner design of type hierarchies z 24 CSC 4101 Programming Languages 8 Structural Subtyping or Retroactive Abstraction class C public int foo interface I int foo z Structural Subtyping I p new C z Retroactive Abstraction class C implements I I p new C 25 Signatures in G signature I int foo class C public int foo I p new C Implemented in G Versions 2 6 2 8 z Use fhandle signatures option z 26 CSC 4101 Programming Languages 9


View Full Document

LSU CSC 4101 - Data Abstraction and OO

Loading Unlocking...
Login

Join to view Data Abstraction and OO and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Data Abstraction and OO and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?