Polymorphism
-
Define the term polymorphism, and explain why it is useful
- Polymorphism is the paradigm where multiple methods, with different implementations or parameters, use the same identifier.
- Polymorphism is useful because it allows a function to call the method without knowing which specific implementation is required; for example, a subclass may need to change the behaviour of a method, or add side-effects; and because it uses the same name, no code change is needed in any function to which it is passed
-
Use the psuedocode below to answer the questions that follow:
we weren't given any pseudocode here...
- State why
Object
cannot override any of the methods inNumber
- All of the methods in
Number
are defined to befinal
(non-virtual)
- All of the methods in
- Identify the name of an overridden method, and explain why it is an overridden method
- No pseudocode, so no names; however, the method will be overridden because it is defined both in the base class (the original implementation) and the subclass (the override)
- Identify the name of an overloaded method
- No pseudocode, so no names; however, the method will be overloaded because it is defined twice, with two different signatures; for example,
add(int, int) -> int
andadd(str, str) -> str
- No pseudocode, so no names; however, the method will be overloaded because it is defined twice, with two different signatures; for example,
- Identify the name of a virtual method, and explain why it is a virtual method
- No pseudocode, so no names; however, the method will be virtual because it is defined in a (base) class, and is not marked
final
(or is markedvirtual
). It can be/is overridden by a subclass
- No pseudocode, so no names; however, the method will be virtual because it is defined in a (base) class, and is not marked
- State why
-
Explain when you would choose to make a method virtual
- A method should be made virtual if it is not essential that a subclasses do not override it; and subclasses that override virtual methods with useful behaviour should call the
super
version of that method in addition to their own logic
- A method should be made virtual if it is not essential that a subclasses do not override it; and subclasses that override virtual methods with useful behaviour should call the
-
Code task