Docsity

Data Abstraction and Object-Oriented - Lecture Slides | CSC 4101, Study notes of Programming Languages

Toggle sidebar
bg1
CSC 4101: Programming Languages 1
1
Data Abstraction and OO
Data Abstraction and OO
Textbook: Chapter 9 (incl. CD material)
2
Data Abstraction and OO
Data Abstraction and OO
zModules or Objects/Classes
zReduces conceptual load by
modularizing code
zContains faults to small parts of code
zMakes program components more
independent
3
Why OO?
Why OO?
zGroups data with code
–Data is isolated in privat e object fields
–Methods operating on the da ta are
bundled with the data in clas ses
–Other code can’t mess with the data
–Classes can be easily added or
removed from the code
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Data Abstraction and Object-Oriented - Lecture Slides | CSC 4101 and more Study notes Programming Languages in PDF only on Docsity!

1

Data Abstraction and OOData Abstraction and OO

Textbook: Chapter 9 (incl. CD material)

2

Data Abstraction and OOData Abstraction and OO

z Modules or Objects/Classes

z Reduces conceptual load by

modularizing code

z Contains faults to small parts of code

z Makes program components more

independent

3

Why OO?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

4

Programming StyleProgramming 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() }

5

OO vs. ADT/Functional StyleOO 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

6

ObjectObject--Oriented ProgrammingOriented Programming

z User-defined types

  • Classes

z Encapsulation

  • Private fields

z Subtype polymorphism

  • Inheritance
  • Structural subtyping

z Code reuse

  • Inheritance

10

Selection of Virtual MethodsSelection 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 */

11

Method Selection AlgorithmMethod 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

12

Implementation of Method DispatchImplementation 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

13

Other Design IssuesOther 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

14

Other Design IssuesOther 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

z Typed or untyped

z Interface types vs. abstract classes

15

Multiple InheritanceMultiple Inheritance

class A { int x; }; class B { int y; public: virtual int foo (int); }; class C : public A, public B { int z; public: virtual int foo (int); virtual void bar (int); };

19

MultimethodsMultimethods

z 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

20

Multimethod ImplementationMultimethod Implementation

z Generic functions dispatch using an

n-dimensional table lookup

z Example

int foo (C); int foo (D); C p = new D(); int i = foo(p); // calls foo(D)

21

Function Call vs. MessageFunction Call vs. Message

PassingPassing

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].

22

Typed vs. UntypedTyped 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

23

Abstract ClassesAbstract Classes

class A { public: virtual int foo () = 0; }; class C : public A { /* ... */ }

z Defines type

z Implementation is supplied in

subclass

24

Interface TypesInterface Types

interface I { int foo (); } class C implements I { /* ... */ }

z Separation of interface and

implementation

z Cleaner design of type hierarchies