Consider the following two statements: (a) A publicly derived class is a…

2015

Consider the following two statements:

(a) A publicly derived class is a subtype of its base class

(b) Inheritance provides for code resuse

Which of the following statements is correct?

  1. A.

    Both statements (a) and (b) are correct

  2. B.

    Neither of the statements (a) and (b) are correct

  3. C.

    Statement (a) is correct and (b) is incorrect

  4. D.

    Statement (a) is incorrect and (b) is correct

Attempted by 315 students.

Show answer & explanation

Correct answer: A

Answer: Both statements (a) and (b) are correct.

Why statement (a) is correct: A publicly derived class is a subtype of its base class because public inheritance expresses an "is-a" relationship. The derived class can be used wherever the base type is expected, enabling polymorphism.

Why statement (b) is correct: Inheritance provides for code reuse since derived classes can reuse and extend the base class's accessible implementation (non-private members).

  • Example: If Shape is a base class and Circle publicly inherits Shape, Circle is a Shape and can reuse Shape's methods.

  • Design note: Reuse via inheritance depends on member accessibility and the correctness of the "is-a" relationship. When reuse is needed without a subtype relationship, prefer composition over inheritance.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Mppsc Assistant Professor