What is polymorphism?
Polymorphism lets code use objects of different concrete types through a shared abstraction while each object supplies its own behavior.
Study 5 Polymorphism and Substitutability with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What is polymorphism?
Polymorphism lets code use objects of different concrete types through a shared abstraction while each object supplies its own behavior.
How does polymorphism separate client code from implementation?
The calling code depends on the shared type, while the object supplied at runtime determines the specific implementation that runs.
What is dynamic dispatch?
Dynamic dispatch, also called late binding, selects an overridable instance method using the object's runtime type.
How do overloading and overriding differ?
Overloading is selected mainly from compile-time argument information; overriding selects inherited instance behavior according to the runtime object.
What is method overriding?
An overriding method provides a compatible implementation of an inherited instance method with the same operation signature.
Why use Java's @Override annotation?
@Override asks the compiler to verify that a method actually overrides an inherited method, catching misspellings or incorrect parameter lists.
What does substitutability require?
Substitutability means an object can be used wherever its abstraction is expected without violating the client's expectations.
How should a subtype handle preconditions?
A subtype should not require more than the supertype requires; otherwise it may reject inputs that valid client code is allowed to provide.
What role does an interface play in polymorphism?
An interface states what clients can request while leaving each implementing class free to determine how the operation is performed.
Why accept the narrowest useful type?
Accept the narrowest useful abstraction, such as Notifier instead of EmailNotifier, so the method depends only on the capability it needs.
How does composition provide polymorphism?
Composition lets a class contain a strategy object, such as a Formatter, so behavior can vary without creating a subclass hierarchy.
Which implementation runs for Shape shape = new Circle(2.0); shape.area();?
The call uses Circle's implementation, because the object is a Circle even though the variable is declared as Shape.