Explore key concepts of single and multiple inheritance in object-oriented programming with scenario-based questions. This quiz clarifies class relationships, method resolution, and potential pitfalls, guiding learners to identify how inheritance models impact code design and behavior.
In a scenario where class Dog inherits only from class Animal, which inheritance type best describes this relationship?
Explanation: Single inheritance occurs when a class derives from only one parent, just like class Dog from class Animal. Multiple inheritance involves inheriting from more than one base class, which is not the case here. Multilevel inheritance refers to a chain (for example, Animal u003E Mammal u003E Dog), and hybrid inheritance combines more than one type of inheritance. Therefore, the relationship described is single inheritance.
If class FlyingFish inherits from both class Fish and class Flyer, which inheritance pattern does this represent?
Explanation: Here, FlyingFish has two direct parents (Fish and Flyer), which is the defining feature of multiple inheritance. Single inheritance permits only one base class. Hierarchical inheritance is when multiple classes share a single parent, and multidimensional inheritance is not a standard term in object-oriented contexts. Only multiple inheritance accurately describes the relationship in the question.
When both parent classes provide the same method and a subclass inherits from them, how does method resolution typically occur with multiple inheritance?
Explanation: In multiple inheritance, the method resolution order (MRO) dictated by the programming language decides which parent's method is called. It is not universally from the rightmost parent, and methods do not usually execute at the same time. The last option is incorrect because the method is still accessible based on the MRO. Thus, the outcome depends on language-specific resolution rules.
If two parent classes provide a method with the same name and a child class inherits from both, what common problem can arise?
Explanation: Unresolved ambiguity occurs when it is unclear which parent’s method should be inherited or run, typical in multiple inheritance scenarios. Name hiding is a different issue related to variable or method scopes. Compilation may actually fail or generate warnings in some languages, so successful compilation is not certain. 'Inheritance doubling' is not a recognized inheritance issue. Therefore, unresolved ambiguity is the main problem here.
Given the need to create a class AmphibiousCar that shares behavior from both Car and Boat, which inheritance type allows directly inheriting from both classes?
Explanation: Multiple inheritance allows a class to have more than one direct parent, such as inheriting features from both Car and Boat in the example. Single inheritance limits a class to one parent. Circular inheritance is problematic and generally not allowed, and duplicate inheritance is not a standard concept. Thus, multiple inheritance is the correct and applicable choice.