Practice
Solution
Step 1: Understand inheritance types
Single inheritance involves one parent and one child class, so it cannot inherit from two unrelated classes.Step 2: Multiple inheritance definition
Multiple inheritance allows a class to inherit from more than one parent class, fitting the scenario.Step 3: Multilevel and hierarchical inheritance
Multilevel is a chain of inheritance, hierarchical is one parent with multiple children, neither fits inheriting from two unrelated classes.Final Answer:
Option B -> Option BQuick Check:
Multiple inheritance is the only type that supports inheriting from multiple unrelated classes.
- Confusing multilevel with multiple inheritance
- Assuming hierarchical inheritance allows multiple parents
- Thinking single inheritance can have multiple parents
Solution
Step 1: Understand polymorphic dispatch
Method calls on subclass instances invoke the subclass's overridden method directly.Step 2: Base class code remains unchanged
The base class is closed for modification; no changes are made to call subclass methods explicitly.Step 3: Why other options are incorrect
The base class method is always executed first, then the subclass method overrides it afterward incorrectly suggests base method runs first then subclass; Both base and subclass methods execute sequentially because the base class is modified to call the subclass implies base class modification; The system duplicates the base class code inside the subclass to avoid modifying the base suggests code duplication, violating DRY and OCP.Final Answer:
Option C -> Option CQuick Check:
Polymorphism enables extension without modifying base code.
- Assuming base method always runs first
- Believing base class must be modified to support extension
- Thinking code duplication is a valid OCP strategy
Solution
Step 1: Review inheritance benefits
Inheritance supports code reuse and polymorphism for uniform handling.Step 2: Recognize limitations
Inheritance hierarchies can become rigid and hard to extend, violating open-closed principle.Step 3: Identify incorrect statement
Claiming inheritance is always best ignores alternatives like composition or interfaces that improve extensibility.Final Answer:
Option C -> Option CQuick Check:
Inheritance is not always best for extensibility [OK]
- Assuming inheritance is always the best design choice
- Ignoring polymorphism benefits
Solution
Step 1: Recall LSP precondition rule
Subclasses must not strengthen preconditions; they can only maintain or weaken them.Step 2: Analyze each statement
A subclass can strengthen preconditions of an inherited method to ensure better input validation. is incorrect because strengthening preconditions breaks substitutability. A subclass must not weaken postconditions of an inherited method. is correct; subclasses can weaken postconditions. Covariance in return types is allowed under LSP. is correct; covariance in return types is allowed. Contravariance in method parameter types is allowed under LSP. is correct; contravariance in parameter types is allowed.Final Answer:
Option A -> Option AQuick Check:
Strengthening preconditions violates LSP.
- Confusing precondition and postcondition rules
- Believing strengthening preconditions is safe
- Misunderstanding covariance and contravariance
Solution
Step 1: Understand overloading resolution
Overloading is resolved at compile time by matching method signatures.Step 2: Identify ambiguity risk
If parameter lists are too similar or implicit conversions exist, the compiler may not decide which method to call, causing ambiguity errors.Step 3: Analyze options
It can cause ambiguity errors at compile time if parameter lists are too similar or implicit conversions apply correctly identifies this compile-time ambiguity risk. It increases runtime overhead due to dynamic dispatch and vtable lookups incorrectly attributes runtime overhead to overloading (it's compile-time). It prevents subclasses from overriding methods with the same name is false; overloading does not prevent overriding. It requires all overloaded methods to have the same return type is false; return types can differ in overloading.Final Answer:
Option A -> Option AQuick Check:
Overloading can cause compile-time ambiguity, not runtime overhead or overriding restrictions.
- Confusing overloading with overriding runtime costs
- Believing overloading restricts subclass method overriding
- Assuming return types must be identical in overloading
