Q: How does inheritance in OOP promote code reusability?
-
A
By preventing the use of existing classes.
-
B
By allowing new classes to be created from existing classes.
-
C
By duplicating code across multiple classes.
-
D
By hiding code from other classes.
B
Answer:
B
Explanation:
Inheritance is a key feature of object-oriented programming that enables a new class to inherit the properties and methods of an existing class. This mechanism promotes code reusability by allowing developers to create new classes that build upon existing ones without rewriting code. When a class inherits from another class, it automatically gains all the features of the parent class, and it can add new features or modify existing ones. This hierarchical relationship reduces redundancy and makes the codebase more manageable and scalable. It also simplifies maintenance and updates, as changes in the parent class can propagate to all derived classes, ensuring consistency across the application.
Related Topic:
Share Above MCQ