In the notification example, why can the same alertUser method work with both EmailNotifier and SmsNotifier?
5 Polymorphism and Substitutability Online Quiz Questions
Use this free practice quiz with 20 questions to review 5 Polymorphism and Substitutability, test your knowledge, and prepare for your next test or exam.
Given Shape shape = new Circle(2.0);, which implementation is used when shape.area() is called?
- A
Circle.area()
- B
Shape.area()
- C
No area method can be called because Shape is abstract.
- D
The compiler chooses a method based only on the variable name.
Which statement correctly distinguishes method overloading from method overriding?
- A
Both terms describe exactly the same mechanism.
- B
Overloading always uses the runtime class, while overriding uses only the compile-time class.
- C
Overloading creates methods with different parameter lists, whereas overriding supplies a subtype implementation of inherited instance behavior.
- D
Overriding is limited to static methods, whereas overloading applies only to instance methods.
True or false: In Java, static methods participate in ordinary instance-method overriding and are selected according to the object's runtime class.
- A
True
- B
False
True or false: Java allows a class to implement multiple interfaces.
- A
True
- B
False
What term describes selecting an overriding instance-method implementation according to the object's runtime type?
Complete the contract rule: A subtype should not require than the supertype requires, and it should preserve the supertype's .
The ReportGenerator example stores a Formatter object and delegates formatting to it. This design uses polymorphism through .
Which two obligations are required for an implementation to be behaviorally substitutable for the abstraction it implements? Select all correct choices.
- A
It should accept every input that the abstraction promises to accept.
- B
It should expose the concrete class so clients can branch on its type.
- C
It should provide the result or effect promised by the abstraction.
- D
It may reject valid inputs if its implementation technology requires it.
Which three practices help client code benefit from polymorphism? Select all correct choices.
- A
Require every caller to construct the concrete implementation directly.
- B
Accept the narrowest useful abstraction as a parameter.
- C
Return an abstraction when practical.
- D
Avoid type tests that replace dispatch.
A FileStore abstraction promises that save accepts any nonempty filename. One implementation rejects filenames containing spaces. What is the best assessment?
- A
It preserves substitutability because rejecting spaces is an implementation detail.
- B
It may violate substitutability by strengthening the operation's precondition.
- C
It improves substitutability because fewer filenames reduce storage errors.
- D
It is valid only if the implementation also changes the method name.
Why can the alertUser method accept both EmailNotifier and SmsNotifier without knowing their concrete classes?
- A
It must contain separate if statements for every notification class.
- B
It can use the shared Notifier contract while the supplied object provides the specific behavior.
- C
It can call private methods declared only by EmailNotifier.
- D
It prevents new notification implementations from being added.
What is the main purpose of Java's @Override annotation?
- A
It lets the compiler verify that the method really overrides an inherited method.
- B
It forces the method to be static.
- C
It makes every subclass method private.
- D
It changes an overloaded method into an interface.
True or false: In Java, a class can implement multiple interfaces even though it normally has only one direct superclass.
- A
True
- B
False
How is an overloaded method normally selected, and how does that differ from an overridden instance method?
- A
It is selected from the object's runtime class after execution begins, whereas an overridden instance method is selected from compile-time argument information.
- B
It is selected from compile-time argument information and parameter lists, whereas an overridden instance method is selected through runtime dispatch based on the object's runtime type.
- C
It is selected only by the method's return type, whereas an overridden instance method is selected randomly when more than one method has the same name.
- D
It is selected randomly when more than one method has the same name, whereas an overridden instance method is selected only by its return type.
A FileStore abstraction accepts any nonempty filename, but one implementation rejects filenames containing spaces. Why can this violate substitutability?
- A
It strengthens the precondition by rejecting an input the abstraction permits.
- B
It preserves substitutability because storage technology may impose any restriction.
- C
It weakens the postcondition by returning a subtype.
- D
It is harmless because subclasses need not honor base-class input rules.
What principle states that properties clients can rely on for a supertype should remain valid when an object of a subtype is substituted?
What is the technical term for an overriding Java method returning a subtype of the original method's return type?
A C# base class defines an instance method that derived classes are expected to replace with specialized implementations. Which keyword or keywords can the base declaration generally use to permit ordinary overriding?
A team defines a Java interface named OfficeMachine with print, scan, and fax methods. A basic printer implements the interface but has to throw an exception from scan and fax because it cannot perform those operations. Explain the design problem and propose a better interface structure.