Chapter 10 - Subclasses and Subtypes
Introduction
- One aspect of polymorphism is that the value held by a variable may not
match the declaration for the variable
- Static type - type assigned at declaration (compile time binding)
- Dynamic type - type associated with the current value of the variable
- When the static type and dynamic type differ, you have polymorphic
variable.
- Subclass - method of constructing new components from existing components
- Subtype - behavior based - if you can substitute class A for class B with
no observable change in behavior, class A is a subtype of class B.
- In dynamic languages concepts of subclass and subtype are not related.
- In "strongly-typed" languages, the distinction is often blurred since
all subclasses are considered subtypes (which may not at all be true.)
- Issues in Binding and Message Lookup
- Method Binding - should method be bound to the message based on the
static type of the receiver or the dynamic type of the receiver
Most of the time we "expect" the dynamic binding but sometimes
the static binding is very useful.
- Reverse Polymorphism Problem - can we "undo" the separation of static
and dynamic types and reassign a value to a variable base on its
dynamic rather that static type
- Substitutability says that we can assign a child to a parent but
reverse polymorphism wants the child to be assigned the parent
- Two problems related to this question - Example of the balls
- 1. Can I tell wether or not the ball coming from the black box
is black or not?
- 2. What mechanism are necessary to assign a value of the ball to
to an instance of a child class?
- Solutions to above problems are language specific - note this is
part of the container problem
- Binding in Programming Lanugages
- Binding and Message Lookup in Object Pascal
- Statically typed
- Assumed that subclasses are subtypes and variables of type parent
can be assigned values of type child
- Objects "know" their own dynamic type so can partly solve the
reverse polymorphism problem.
- Always used dynamic binding of method to message but legality is
determined at compile time
- Binding and Message Lookup in Smalltalk
- Dynamically typed therefore dynamic binding of method to message
- Reverse polymorphism problem does not exist.
- All objects "know" their type
- If receiver does not understand message, a run-time error occurs
- Binding and Message Lookup in Objective-C
- Basically a dynamically typed language - very Smalltalk like in
binding.
- Dynamically typed objects use dynamic binding
- Have option of having some statically typed objects and messages
will also be static
- Binding and Message Lookup in C++
- Objective of the language design was to backwardly compatible with
C and to be both time and space efficient. No feature should
incur a cost unless it is actually used.
- Static language
- Only pointers can be used for polymorphism (they are the only
polymorphic variables.) - no substitutability without pointers.
- Method binding - static for regular variables, only pointers have
dynamic binding BUT...
- The method to be executed is based on the dynamic type of the
pointer ONLY if the parent class has specified that the dynamic
type is to be used. Otherwise, the static type of the pointer
is used to determine which method is to be used.
- The parent class must specify that the dynamic type will be used to
determine which method (parents or childs) to use of some but not
necessarily all its methods can take place by designating the
individual methods as virtual.
- At compile time, the static type is used to determine whether or
not a message is legal.
- I did find information for type information in the standard.
- Using the typeid info, the objects "know" their type so can "solve"
reverse polymorphism problem to some extent.
- Binding and Message Lookup in Java
- All variables know their dynamic type
- Assumed that subclasses are subtypes
- Explicit cast required to assign a "parent" to a "child" - reverse
polymorphism problem
- Run time exception if the cast is not valid
- Message-Method is dynamic and bound to dynamic type
- If data field overridden, then static type is used for method-message
binding
- Uses concept of interface to separate idea of subclass and subtype
- Merits of Static versus Dynamic Binding
- Relative merits arguments come down to efficiency vs. flexibility
- Static is efficient, dynamic is flexible
- +Dynamic typing is more "OOP" like since each object knows its type
- +Dynamic - Easy to create general-purpose data structures
- -Dynamic - run-time search to determine which method to execute is
expensive and errors not discovered until run-time - substantial expense
- +Static - writing the compiler for static language is easier
- +Static - storage allocation is fast and code is faster
- -Static - most objects lose their self-knowledge - reverse polymorphism
problem requires extra stuff if it is to be solved
- -Static - writing general purpose software is difficult
- +Static - method binding when static is very fast and if the binding
is done at compile time then can be implemented as a procedure call
- -Dynamic - always requires some run-time lookups and thus run-time
errors when there is no match for method and message
- Bottom line - for your application, is efficiency or flexibility the
most important consideration
- OOP is NOT a panacea - it is just another tool that needs to be used
when the problem to be solved fits the paradigm. Not all problems
should be solved using OOP techniques.