What is the main advantage of using abstract classes over interfaces?
2025
What is the main advantage of using abstract classes over interfaces?
- A.
Abstract classes can have multiple inheritance, unlike interfaces
- B.
Interfaces can have constructors, while abstract classes cannot
- C.
Abstract classes can provide default implementations of methods
- D.
Interfaces allow for private access modifiers, unlike abstract classes
Attempted by 275 students.
Show answer & explanation
Correct answer: C
Abstract classes and interfaces are both used to achieve abstraction in object-oriented programming, but they differ in key ways.
An abstract class can provide default implementations for methods, allowing derived classes to inherit and optionally override them. This is not possible with interfaces, which only define method signatures.
Abstract classes support single inheritance, meaning a class can extend only one abstract class. Interfaces, on the other hand, allow multiple inheritance, as a class can implement multiple interfaces.
Abstract classes can have constructors, which are used to initialize state. Interfaces cannot have constructors.
Access modifiers like private, protected, and public can be used in abstract classes. Interfaces do not support access modifiers, as all methods are implicitly public.
The main advantage of abstract classes is the ability to provide default method implementations and shared state through constructors and fields.
हिन्दी उत्तर:
एब्स्ट्रैक्ट क्लास और इंटरफेस दोनों ऑब्जेक्ट-ओरिएंटेड प्रोग्रामिंग में अब्स्ट्रैक्शन प्राप्त करने के लिए उपयोग किए जाते हैं, लेकिन वे मुख्य रूप से अलग होते हैं।
एब्स्ट्रैक्ट क्लास में विधियों के डिफ़ॉल्ट लागू करने की क्षमता होती है, जिससे व्युत्पन्न वर्ग उन्हें विरोध कर सकते हैं। इंटरफेस में ऐसा नहीं होता है, जो केवल विधि साइनेचर परिभाषित करता है।
एब्स्ट्रैक्ट क्लास सिंगल विरासत का समर्थन करता है, जिसका अर्थ है कि एक वर्ग केवल एक एब्स्ट्रैक्ट क्लास विरासत ले सकता है। इंटरफेस, दूसरी ओर, बहुविरासत की अनुमति देता है, क्योंकि एक वर्ग कई इंटरफेस को लागू कर सकता है।
एब्स्ट्रैक्ट क्लास में कंस्ट्रक्टर हो सकते हैं, जो राज्य को शुरू करने के लिए उपयोग किए जाते हैं। इंटरफेस में कंस्ट्रक्टर नहीं हो सकते हैं।
एब्स्ट्रैक्ट क्लास में जैसे कि प्राइवेट, प्रोटेक्टेड और पब्लिक जैसे एक्सेस मॉडिफायर का उपयोग किया जा सकता है। इंटरफेस में एक्सेस मॉडिफायर नहीं होते हैं, क्योंकि सभी विधियां निहित रूप से पब्लिक होती हैं।
एब्स्ट्रैक्ट क्लास का मुख्य लाभ डिफ़ॉल्ट विधि लागू करने और फ़ील्ड्स के माध्यम से साझा राज्य प्रदान करने की क्षमता है।