Concept of Interfaces and Abstract classes

Abstract Classes in C++

Abstract Class वह class होती है, जिसमें कम से कम एक pure virtual function होता है। इसे आप partially defined class भी कह सकते हैं।

Key Points 

  • Abstract class में कम से कम एक pure virtual function होना अनिवार्य है।
  • Abstract class का direct object नहीं बनाया जा सकता।
  • At least एक abstract method होना जरूरी है।
  • Derived class में abstract methods को override करना mandatory है।
  • यह constructor और normal methods रख सकती है।
  • यह variables और concrete methods भी allow करती है।

Syntax

#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() = 0; // pure virtual function
void display() {
          cout << “This is a Shape” << endl;
      }
};

Example (उदाहरण)

class Ticket {
public:
        virtual void bookTicket() = 0; // Abstract function
};
class TrainTicket : public Ticket {
public:
       void bookTicket() {
           cout << “Train ticket booked!” << endl;
}
};
class FlightTicket : public Ticket {
public:

       void bookTicket() {
           cout << “Flight ticket booked!” << endl;
 }
};

Advantages of Abstract Classes (लाभ)

  • Abstract Class partial abstraction allow करती है।
  • यह code reusability बढ़ाती है, क्योंकि common logic base class में लिख सकते हैं।
  • Abstract class में constructors allowed होते हैं, जिससे initial setup करना आसान होता है।
  • इसमें normal (concrete) methods भी हो सकते हैं, जो default behavior provide करते हैं।
  • Encapsulation strong होता है क्योंकि base class important logic hide कर सकती है।
  • Related classes के लिए common template provide करती है, जिससे code structure clean रहता है।
  • Runtime polymorphism achieve करना आसान हो जाता है।

Disadvantages of Abstract Class (हानियाँ)

  • Abstract Class multiple inheritance support नहीं करती, इससे flexibility कम हो जाती है।
  • कभी-कभी abstract class के कारण classes के बीच tight coupling बन जाती है।
  • बड़ी hierarchy में abstract classes manage करना थोड़ा complex और confusing हो सकता है।
  • Interfaces के comparison में ये 100% abstraction provide नहीं करती।
  • अगर abstract methods ज्यादा हों तो child class पर implementation load बढ़ जाता है।

Interface Concept in C++

Interface एक pure abstract structure होता है, जिसमें सभी methods abstract होते हैं।

Key Points 

  • Interface में सिर्फ method declaration होती है।
  • Object direct नहीं बन सकता।
  • Multiple inheritance support करता है।
  • Variables default constant/static होते हैं।

Syntax

class InterfaceName {
public:
            virtual void function1() = 0; // Pure virtual function
            virtual void function2() = 0; // Pure virtual function
};

Example (उदाहरण)

class InterfaceName {
public:
     virtual void show() = 0;
     virtual void display() = 0;
};

// Implementing the interface
class Demo : public InterfaceName {
public:
     void show() override {
          cout << “Show function implemented” << endl;
}

     void display() override {
          cout << “Display function implemented” << endl;
}
};

Advantages of Interface (लाभ)

  • Interface 100% Abstraction provide करता है, यानी पूरा design साफ, structured और rules-based बनता है।
  • यह Multiple Inheritance को support करता है, जिससे code की flexibility और reusability बढ़ती है।
  • Interfaces system में Loose Coupling create करते हैं, क्योंकि classes सिर्फ interface के rules follow करती हैं और उनकी implementation अलग-अलग हो सकती है।
  • Team work आसान होता है, हर developer एक ही common interface contract को follow करता है।
  • Future में नई classes add करना simple है— interface को implement करना होता है |
  • Interface-based design software को ज्यादा modular, clean और maintainable बनाता है।
  • एक ही interface को multiple classes अलग-अलग behavior के साथ implement कर सकती हैं, जिससे polymorphism improve होता है।

Disadvantages of Interface (हानियाँ)

  • Interface में कोई भी implementation नहीं होता, इसलिए हर class को इसके सभी methods की body खुद लिखनी पड़ती है।
  • Interface के methods default रूप से public होते हैं, जिससे access control कम हो जाता है।
  • Interface के variables हमेशा public static final होते हैं, इससे flexibility  कम हो जाती है।
  • Abstract class की तुलना में interface में code reusability कम होती है, क्योंकि common code define नहीं किया जा सकता।
  • अगर project में बहुत ज्यादा interfaces हों, तो पूरा structure complex और confusing बन सकता है।
  • Interface में change करने पर सभी implementing classes में changes करने पड़ते हैं, जिससे maintenance load बढ़ जाता है।

Differences Between Abstract Class and Interface
(Abstract Class और Interface में अंतर)

FeatureAbstract Class (C++)Interface (Pure Abstract Class in C++)
Object CreationAbstract class का object directly create नहीं किया जा सकता।Interface का object भी directly create नहीं किया जा सकता।
MembersAbstract class में data members और functions दोनों हो सकते हैं।Interface में सिर्फ pure virtual functions होते हैं, कोई data member नहीं।
ImplementationAbstract class में implemented methods भी हो सकते हैं।Interface में कोई implementation नहीं होती।
PurposePartial implementation और base class functionality provide करना।Complete abstraction और contract define करना।
InheritanceSingle या multiple inheritance possible, लेकिन complex हो सकता है।Multiple inheritance आसान है, क्योंकि केवल function signatures inherit होते हैं।
Constructors / Destructorsहो सकते हैं, लेकिन object create नहीं कर सकते।हो सकते हैं, लेकिन object create नहीं कर सकते।
Use CaseBase class जिसमें common code reuse करना हो।जब sirf method signatures define करने हों और multiple classes में implement करवानी हों।
Exampleclass Ticket { virtual void book()=0; void showInfo(){...} };class PaymentInterface { virtual void pay()=0; };

 

error: Content is protected !!