- 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.
- Modules.
- The classic example is Ada, where they are called 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 (from 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
- Generic classes.
- Java generics.
- C++ templates.
- Modules v. Classes
- Both allow the programmer to create variables representing a
real-world object or data structure.
- Specify data which describes this object
- Specify methods to operate on it.
- Methods have access which is denied to clients.
- A class
- is a type name used to declare objects.
- Methods are considered part of the object, and called as members:
Internal notation
- A module
- is not a type name, but will contain the definition
of one or more appropriate type names used to declare objects.
- Methods are not part of the object, but receive objects as
parameters to operate on:
External notation
- 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
- Classes can inherit not modules do not.
- Classes define types, so inheritance makes sense to create new
types.
- Modules (may contain) but do not define types, so in heritance of
modules does not make sense.
- A module can use or import another, allowing to start with the
earlier definitions and extend.
- 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.