📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    When should you use inheritance over

    composition?
    A When you want to reuse code without establishing an "is-a" relationship. Correct Answer Incorrect Answer
    B When the relationship between classes is "has-a". Correct Answer Incorrect Answer
    C When the subclass is a specialized type of the superclass ("is-a" relationship). Correct Answer Incorrect Answer
    D When you need to achieve multiple inheritance of implementation. Correct Answer Incorrect Answer
    E When you want to avoid polymorphism. Correct Answer Incorrect Answer

    Solution

    Inheritance is appropriate when there's a clear "is-a" relationship, meaning the subclass is a specific kind of the superclass and can substitute for it (Liskov Substitution Principle). Composition ("has-a" relationship) is generally preferred for code reuse when the "is-a" relationship doesn't hold.

    Practice Next
    ask-question