ProgrammingHardGATE CS · TCS NQT Prime · Product Company SDE
Why did Java and C# choose single inheritance of classes plus multiple inheritance of interfaces, instead of C++-style full multiple inheritance of classes?
- A. Because Java does not support any form of inheritance
- B. To avoid the diamond problem's ambiguity over which base class's state or implementation to use, while still allowing a type to satisfy several behavioural contracts via interfaces
- C. Because full multiple class inheritance was patented by another company
- D. Because interface method calls execute faster than class method calls
Correct answer: B. To avoid the diamond problem's ambiguity over which base class's state or implementation to use, while still allowing a type to satisfy several behavioural contracts via interfaces
Multiple inheritance of implementation creates ambiguity over shared state and conflicting method bodies (the diamond problem); interfaces side-step this because they (traditionally) carry no state.
More OOP with C++ and Java questions
- Which statement correctly distinguishes an abstract class from an interface in Java (interfaces may have default/static/private methods…
- Within a single derived class's own constructor body, in what order are its direct base class subobjects (assuming no virtual inheritance)…
- Which statement correctly describes the typical cost of exceptions in C++?
- Why can a generic method not simply write new T[10] for a type parameter T in Java?
- java void f(int x) { System.out.print("int"); } void f(Integer x) { System.out.print("Integer"); } void f(Object... x) {…
- Why does adding a brand-new virtual function to a base class not increase the size of each object in that hierarchy (assuming objects…