Object-oriented programming: Is multilevel inheritance allowed (A → B → C → D)? Select the statement that best reflects C++/Java-style inheritance rules.
-
ACorrect
-
BIncorrect
-
COnly with interfaces
-
DOnly with abstract classes
-
ERuntime dependent
Answer
Correct Answer: Correct
Explanation
Introduction / Context: Multilevel inheritance means a class derives from a class that itself derives from another class, forming a chain such as A → B → C → D. This is a foundational capability in classical OOP languages like C++ and is conceptually supported in Java via single inheritance of classes (interfaces are separate). The question tests whether such layered hierarchies are permitted.
Given Data / Assumptions:
- We consider typical OOP languages with single inheritance of implementation for classes (e.g., C++, Java).
- The chain A → B → C → D represents class D extending C, which extends B, which extends A.
- No multiple-inheritance-of-classes is being asserted—only multilevel (transitive) inheritance.
Concept / Approach: In multilevel inheritance, every class in the chain inherits the accessible members of its parent. Constructors and destructors (or finalizers) are invoked in well-defined orders. Method overriding, virtual dispatch (C++) or dynamic dispatch (Java), and polymorphism apply transitively.
Step-by-Step Solution:
Define base A with some interface/behavior.Define B : A to inherit and extend A.Define C : B, further extending behavior.Define D : C, completing the chain. This is valid and common.Verification / Alternative check: Many standard libraries organize types in inheritance chains (e.g., Java’s java.io hierarchy). Unit tests that instantiate D can call methods from A, B, and C according to access rules.
Why Other Options Are Wrong:
- Incorrect: Denies a supported language feature.
- Only with interfaces / Only with abstract classes: Not required; concrete classes may participate.
- Runtime dependent: This is a compile-time language feature, not runtime dependent.
Common Pitfalls: Confusing multilevel inheritance (legal) with multiple inheritance of classes (restricted in Java). Poorly designed deep chains can harm maintainability, but that is a design smell, not a prohibition.
Final Answer: Correct