Interface in OOP
Understanding contract-based programming and multiple inheritance in Object-Oriented Programming
What is an Interface?
An interface is a contract that defines a set of methods that a class must implement. It provides a way to achieve abstraction and multiple inheritance in OOP. In C++, interfaces are implemented using abstract classes with pure virtual functions.
Interface क्या है? (हिंदी में)
Interface एक contract है जो methods का एक set define करता है जिसे एक class को implement करना होता है। यह OOP में abstraction और multiple inheritance को achieve करने का एक तरीका है। C++ में interfaces को abstract classes और pure virtual functions का उपयोग करके implement किया जाता है।
Key Points:
- Contract: Defines what methods a class must implement
- Abstraction: Hides implementation details
- Multiple Inheritance: A class can implement multiple interfaces
- Pure Virtual: All interface methods are pure virtual functions
Types of Interfaces
Pure Interface
Contains only pure virtual functions
Abstract Interface
Contains both pure virtual and concrete methods
Marker Interface
Empty interface used for type identification
Simple Example
#include <iostream> #include <string> // Interface 1: Printable class Printable { public: virtual void print() const = 0; virtual ~Printable() = default; }; // Interface 2: Drawable class Drawable { public: virtual void draw() const = 0; virtual ~Drawable() = default; }; // Interface 3: Resizable class Resizable { public: virtual void resize(double factor) = 0; virtual ~Resizable() = default; }; // Class implementing multiple interfaces class Circle : public Printable, public Drawable, public Resizable { private: double radius; std::string color; public: Circle(double r, const std::string& c) : radius(r), color(c) {} // Implementing Printable interface void print() const override { std::cout << "Circle with radius " << radius << " and color " << color << std::endl; } // Implementing Drawable interface void draw() const override { std::cout << "Drawing a " << color << " circle with radius " << radius << std::endl; } // Implementing Resizable interface void resize(double factor) override { if (factor > 0) { radius *= factor; std::cout << "Circle resized. New radius: " << radius << std::endl; } } // Additional methods double getArea() const { return 3.14159 * radius * radius; } }; int main() { // Create a circle object Circle circle(5.0, "Red"); // Use through different interfaces Printable* printable = &circle; Drawable* drawable = &circle; Resizable* resizable = &circle; // Call interface methods std::cout << "Using Printable interface:" << std::endl; printable->print(); std::cout << "\nUsing Drawable interface:" << std::endl; drawable->draw(); std::cout << "\nUsing Resizable interface:" << std::endl; resizable->resize(2.0); // Use concrete class methods std::cout << "\nUsing concrete class:" << std::endl; std::cout << "Area: " << circle.getArea() << std::endl; return 0; }
उदाहरण की व्याख्या (Example Explanation)
इस उदाहरण में interfaces का उपयोग किया गया है:
- Multiple Interfaces: Circle class तीन interfaces implement करता है
- Interface Methods: हर interface में pure virtual functions हैं
- Implementation: Circle class सभी interface methods को implement करता है
- Interface Usage: Base class pointers के through interface methods को call करते हैं
Benefits of Interfaces
Interfaces के फायदे
-
कोड का लचीलापन (Code Flexibility)
- Multiple inheritance को support करता है
- Classes को loosely coupled बनाता है
-
कोड का पुनः उपयोग (Code Reusability)
- Common functionality को share करने में मदद करता है
- Code duplication को कम करता है
-
कोड का रखरखाव (Code Maintenance)
- Implementation changes को आसानी से कर सकते हैं
- Code को maintain करना आसान होता है
Best Practices
- Keep interfaces small and focused
- Use interfaces for type identification
- Prefer composition over multiple inheritance
- Make interface methods pure virtual
- Provide virtual destructors in interfaces