Modular and Class Abstraction
  1. Encapsulation
    1. Allows logically related constants, types, variables, methods, etc. to be grouped.
    2. Examples include older constructs: functions, procedures, and structs.
    3. Became clear that data types need to be encapsulated: modules, packages, and classes.
  2. Modules.
    1. The classic example is Ada, where they are called packages.
    2. Collect data and operations into a named scope.
    3. Ada integer stack package: package, body, simple driver.
  3. Generic Packages.
    1. Allows types and other parameters to a package.
    2. Ada generic stack (from Types): package, body, simple driver.
  4. Classes.
    1. Packages, plus:
      1. Class names are types.
      2. Classes can be instantiated.
      3. 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.
    2. Translating a class
  5. Generic classes.
    1. Java generics.
    2. C++ templates.
  6. Modules v. Classes
    1. Both allow the programmer to create variables representing a real-world object or data structure.
      1. Specify data which describes this object
      2. Specify methods to operate on it.
      3. Methods have access which is denied to clients.
    2. A class
      1. is a type name used to declare objects.
      2. Methods are considered part of the object, and called as members: Internal notation
    3. A module
      1. is not a type name, but will contain the definition of one or more appropriate type names used to declare objects.
      2. Methods are not part of the object, but receive objects as parameters to operate on: External notation
  7. Derived classes.
    1. New class extends an existing class.
    2. Implementation
      1. Just create new dynamic struct with added fields.
      2. A pointer to the derived class is a pointer to the base class part.
      3. Translating
    3. Classes can inherit not modules do not.
      1. Classes define types, so inheritance makes sense to create new types.
      2. Modules (may contain) but do not define types, so in heritance of modules does not make sense.
      3. A module can use or import another, allowing to start with the earlier definitions and extend.
  8. Inheritance polymorphism.
    1. An derived-class object retains its behavior even when referred to by a base-class reference.
    2. Dynamic binding: The code which a function call executes is determined at run time, by the type of object.
    3. Translating.
    4. In Java, all methods are polymorphic; in C++, only those declared virtual.
      1. Using the vtable costs extra space and execution time.
      2. 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.
      3. The C++ design goal emphasizes speed. Its design allows the the programmer to decide if the overhead is needed.
  9. Run-time type identification
    1. 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.
    2. An object function returns the class. Java: obj.getClass()
      1. Usually a string in interpreted languages.
        In Ruby, get a class object.
      2. Usually a numeric code in compiled languages.
    3. Some syntax to ask if two object have the same type, or share a base class.
    4. C++ RTTI is late feature and only works when the class has virtual functions.
  10. Reflection: Can find out all methods, data, etc about a class at run-time.
  11. Template classes
    1. Allows types and other parameters to a package.
    2. C++ stack template: header, test driver.
  12. Multiple inheritance.
    1. What you might want it for
      1. Create a GUI object which displays the contents of a queue: It is a Queue and it is a Window.
      2. Personnel database: Person is a SalariedEmployee; Person is a Accountant.
    2. Support
      1. Most languages do not support MH.
      2. C++ does.
      3. Approximations
        1. Java interfaces.
        2. Ruby mix-ins.
    3. Problems with MI
      1. Same function is defined differently in multiple base classes.
      2. Some class becomes and ancestor multiple times.
  13. Flavors
    1. In a pure object-oriented language, everything is an object.
      1. The original: Smalltalk.
      2. Ruby, pretty much
        1. In Ruby, if is an actual statement.
        2. In Smalltalk, iftrue: is a message you send to a boolean.
      3. Ruby uses syntactic tricks to make some object thinks look otherwise.
    2. Java is not pure object-oriented
      1. int, double, etc.
      2. Classes are not objects.
    3. C++, Python, and many others are mixed-paradigm, since they have top-level functions and such.
    4. Static typing an polymorphism don't mix well.