Variations on Inheritance Flashcards
What’s an abstract class?
A class that cannot be instantiated
Can abstract classes have constructors?
yes. if a concrete class derives from the abstract, when constructing the concrete class we need to construct the abstract class first.
What’s an abstract method?
a method without implementation, postponed to inheriting class.
What’s the difference between pure virtual function in c++ and an abstract method in java or c#?
PVF: may have implementation
abstract method: doesn’t have implementation
What’s the difference between interface and abstract class in Java?
Interface:
only abstract methods and const definitions.
pure protocol specification.
Abstract class: methods may have default implementation. every class with an abstract method is abstract. not every abstract class has an abstract method.
What’s a final method?
cannot be overridden
What’s a final class?
cannot be further derived
What’s multiple dispatch?
the executed method is selected by the dynamic type of one (or more) argument(s)
What’s MultiMethod?
a method that takes part in multiple dispatch
What are the Drawbacks of MultiMethods?
- Expensive dispatch process
Must examine dynamic types of all arguments - Possible run time ambiguity
Given A::f(A, A), B::f(B, A) and B::f(A, B)
What happens if you pass two B’s to A::f? - Can get the same functionality in standard Java
What is a binary method?
a method that takes as an argument a parameter of the same type as the class the method is declared in.
How mixins are implemented in c++?
Using templates. let's say we want to add an undo functionality to different classes. let's use a mixin: typedef UndoableMixin UndoableInt; typedef UndoableMixin Undoable char;
the new functionality will be in UndoableMixin.
What are the drawbacks of c++’s mixins?
mixin is not a class.
What is the drawback of mixins in java?
Adding a method to a mixin may silently override an inherited method
What’s a trait?
A composable unit of behavior. Serves as a type No fields Provides some methods (with behavior) Requires other methods (abstract) When composing traits, if a method has more than one implementation it becomes abstract