Modular and Class Abstraction
- Abstract Data Types
- Combine data definition and operations.
- Plain C integer stack:
header
implementation
simple driver
- Parts relate mostly in the programmer's head.
- Encapsulation
- Allows logically related constants, types, variables, methods, etc. to
be grouped.
- Examples include older constructs: functions, procedures, and structs.
- Became clear that data types need to be encapsulated: modules, packages,
and classes.
- Packages.
- Collect data and operations into a named scope.
- Ada integer stack package:
package
body
simple driver.
- Generic Packages.
- Allows types and other parameters to a package.
- Ada generic stack (seen before under types):
package
body
simple driver.
- Classes.
- Packages, plus:
- Class names are types.
- Classes can be instantiated.
- Syntax for calling methods of an instance.
x.f(a,b,c)
becomes roughly class::f(x,a,b,c), where the first
arg is just the this pointer.
- Translating a class
- Derived classes.
- New class extends an existing class.
- Implementation
- Just create new dynamic struct with added fields.
- A pointer to the derived class is a pointer to the base class part.
- Translating
- Inheritance polymorphism.
- An derived-class object retains its behavior even when referred to
by a base-class reference.
- Dynamic binding: The code which a function call executes is determined
at run time, by the type of object.
- Translating.
- In Java, all methods are polymorphic; in C++, only those declared
virtual.
- Using the vtable costs extra space and execution time.
- Not knowing at compile time which method will be called prevents
optimization based on in-lining the body. Since classes
promote the use of small methods, this can be significant.
- The C++ design goal emphasizes speed. Its design allows the
the programmer to decide if the overhead is needed.
- Run-time type identification
- Can tell the type of an object at run time.
In a statically-typed language, this is the dynamic type. There is
also a static type.
- An object function returns the class. Java: obj.getClass()
- Usually a string in interpreted languages.
In Ruby, get a class object.
- Usually a numeric code in compiled languages.
- Some syntax to ask if two object have the same type, or share a base
class.
- C++ RTTI is late feature and only works when the class has virtual
functions.
- Reflection: Can find out all methods, data, etc about a class at run-time.
- Template classes
- Allows types and other parameters to a package.
- C++ stack template:
header,
test driver.
- Multiple inheritance.
- What you might want it for
- Create a GUI object which displays the contents of a queue: It is a
Queue and it is a Window.
- Personnel database: Person is a SalariedEmployee;
Person is a Accountant.
- Support
- Most languages do not support MH.
- C++ does.
- Approximations
- Java interfaces.
- Ruby mix-ins.
- Problems with MI
- Same function is defined differently in multiple base classes.
- Some class becomes and ancestor multiple times.
- Flavors
- In a pure object-oriented language, everything is an object.
- The original: Smalltalk.
- Ruby, pretty much
- In Ruby, if is an actual statement.
- In Smalltalk, iftrue: is a message you send to a boolean.
- Ruby uses syntactic tricks to make some object thinks look otherwise.
- Java is not pure object-oriented
- int, double, etc.
- Classes are not objects.
- C++, Python, and many others are mixed-paradigm, since they have
top-level functions and such.
- Static typing an polymorphism don't mix well.