Inheritance and Polymorphism Flashcards

1
Q

what is ad-hoc pulymorphism?

A
  • 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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what is universal polymorphism?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is strict inheritance?

A
A limited form of inheritance:
extend a given class without touching its code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what’s the connection between strict inheritance and the class parts?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

pros and cons of strict inheritance?

A
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…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what are polymorphic variables?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is up-casting?

A

casting pointers up the
inheritance hierarchy.
this pointer is up-casted implicitly whenever an inherited method is called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is down-casting?

A

casting pointers and
references down the inheritance hierarchy.

must be done explicitly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

does Java have array subtyping?

A
yes! 
in Java, if class Manager is a subtype of class Employee, then Manager[] is a subtype of Employee[]:
How well did you know this?
1
Not at all
2
3
4
5
Perfectly