Inheritance and Polymorphism Flashcards
what is ad-hoc pulymorphism?
- Polymorphism is over few (often, very few) shapes
- Different shapes are generated manually (or semi- )
- No unifying common ground to all shapes, other than designer’s intentions
- Uniformity is a coincidence, not a rule
two types: overloading (defined by progammer) and coercion(defined by language/compiler).
what is universal polymorphism?
Polymorphism is over infinitely many types
There is a unifying, common ground to all the different shapes the polymorphic entity may take
Parametric:
template void Sort(T list) …
Relies on templates/generics
Inclusion:
Base* b = new Derived();
Relies on inheritance
what is strict inheritance?
A limited form of inheritance: extend a given class without touching its code
what’s the connection between strict inheritance and the class parts?
Forge
- The derived class has a new forge
Mill
- The derived class has a new mill
Usually, must invoke the mill of the base class
Protocol
- The derived class can add protocol elements
Behavior
- The derived class implements only the new protocol elements
Structure
- The structure of the derived class is an extension of the base class
pros and cons of strict inheritance?
Benefits of strict inheritance: 1. No performance penalty Compile-time creature 2. No conceptual penalty Structured path for understanding the classes 3. Conformance (AKA substitutability) If a class B inherits from another class A, then the objects of B can be used wherever the objects of A are used. I.e., B is an A.
Drawbacks of strict inheritance:
Not overly powerful…
what are polymorphic variables?
- Pointers/references of polymorphic types
- All variables in Smalltalk are polymorphic. They may store instances of all classes.
- this is a polymorphic variable. It may point to things of different subtypes at different times.
- A pointer to an inherited type is generated whenever an inherited method is called.
- In fact, all class pointers and all class references in C++ are polymorphic
what is up-casting?
casting pointers up the
inheritance hierarchy.
this pointer is up-casted implicitly whenever an inherited method is called.
what is down-casting?
casting pointers and
references down the inheritance hierarchy.
must be done explicitly.
does Java have array subtyping?
yes! in Java, if class Manager is a subtype of class Employee, then Manager[] is a subtype of Employee[]: