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?
- A.
Both statements (a) and (b) are correct
- B.
Neither of the statements (a) and (b) are correct
- C.
Statement (a) is correct and (b) is incorrect
- 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.