2191
GB
What is the fundamental difference between 'composition' and 'inheritance' as reuse mechanisms in OOP?
IN
OOP में पुन: उपयोग तंत्र के रूप में 'संरचना' और 'विरासत' के बीच मूलभूत अंतर क्या है?
A
Composition only with interfaces; inheritance requires abstract classes
केवल इंटरफेस के साथ रचना; विरासत के लिए अमूर्त वर्गों की आवश्यकता होती है
B
Inheritance is static compile-time reuse creating 'is-a' relationships exposing parent internals; composition is dynamic runtime 'has-a' relationship that is more flexible, testable, and avoids the fragile base class problem
वंशानुक्रम स्थैतिक संकलन-समय का पुन: उपयोग है जो मूल आंतरिक को उजागर करने वाले 'ए-ए' संबंध बनाता है; रचना गतिशील रनटाइम 'है-ए' संबंध है जो अधिक लचीला, परीक्षण योग्य है, और नाजुक बेस क्लास समस्या से बचाती है
C
Composition always produces faster code than inheritance
कंपोज़िशन हमेशा इनहेरिटेंस की तुलना में तेज़ कोड उत्पन्न करता है
D
Both are equivalent; choice depends only on preference
दोनों समतुल्य हैं; चुनाव केवल प्राथमिकता पर निर्भर करता है
✅ Correct Answer:
💡 Explanation / व्याख्या
Explanation (English)
The 'fragile base class problem' shows inheritance's weakness: changing a parent class can break subclasses unexpectedly. GoF's 'favour composition over inheritance' recommends building objects from collaborating components, enabling runtime behaviour changes through dependency injection.
व्याख्या (हिन्दी)
'नाजुक आधार वर्ग समस्या' वंशानुक्रम की कमजोरी को दर्शाती है: मूल वर्ग को बदलने से उपवर्ग अप्रत्याशित रूप से टूट सकते हैं। GoF की 'विरासत पर अनुकूल संरचना' निर्भरता इंजेक्शन के माध्यम से रनटाइम व्यवहार परिवर्तन को सक्षम करने, सहयोगी घटकों से वस्तुओं के निर्माण की सिफारिश करती है।