28

Possible Duplicate:
How do you declare an interface in C++?
Interface as in java in c++?

I am a Java programmer learning C++, and I was wondering if there is something like Java interfaces in C++, i.e. classes that another class can implement/extend more than one of. Thanks. p.s. New here so tell me if I did anything wrong.

Community
  • 1
  • 1
nickbrickmaster
  • 389
  • 1
  • 3
  • 4
  • 11
    C++ supports full [multiple inheritance](http://en.wikipedia.org/wiki/Multiple_inheritance), so there's no distinction between classes and interfaces like in Java. If you make a class with all pure virtual methods then it's the equivalent of a Java interface. – DaoWen Aug 14 '12 at 04:57
  • 1
    An abstract class with only pure virtual member functions. – obataku Aug 14 '12 at 04:57
  • Also consider the VC++'s '__interface' keyword. check MSDN. – MasterMastic Aug 14 '12 at 04:58
  • This question http://stackoverflow.com/questions/6831161/interface-as-in-java-in-c is even more exact a duplicate (but has itself been closed as duplicate of the one I linked above). – jogojapan Aug 14 '12 at 05:24
  • @DaoWen "_If you make a class with all pure virtual methods then it's the equivalent of a Java interface._" almost. See my answer. – curiousguy Aug 14 '12 at 06:09
  • @close-voters: you have prevented any real answer to the question, by closing this as **a duplicate of a duplicate that has itself been closed**, the latter as an alleged duplicate of a just vaguely related question. when you feel the urge to close a question as a duplicate of a closed question, please try harder to not act on that impulse. thanks. – Cheers and hth. - Alf Aug 14 '12 at 09:10

2 Answers2

35

In C++ a class containing only pure virtual methods denotes an interface.

Example:

// Define the Serializable interface.
class Serializable {
     // virtual destructor is required if the object may
     // be deleted through a pointer to Serializable
    virtual ~Serializable() {}

    virtual std::string serialize() const = 0;
};

// Implements the Serializable interface
class MyClass : public MyBaseClass, public virtual Serializable {
    virtual std::string serialize() const { 
        // Implementation goes here.
    }
};
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
StackedCrooked
  • 34,653
  • 44
  • 154
  • 278
  • 4
    You need **virtual inheritance** to emulate Java interface implementation. FTFY. – Cheers and hth. - Alf Aug 14 '12 at 05:30
  • 1
    -1, it's *abstract class*, not *interface*. You can't own object via it's *interface*, i.e. *interface* can't have public destructor. (unless it's the `IDestructible` *interface*). – Abyx Aug 14 '12 at 08:51
  • 2
    @Abyx Is that Java specific? In C++ you can delete an object through a pointer to an interface just fine (if it's destructor is declared virtual). Whether or not that's appropriate depends on the application. If it was not appropriate then making the destructor protected would enforce the rule. – StackedCrooked Aug 15 '12 at 03:02
  • @Abyx "_interface can't have public destructor_" Source? What is the **official** definition for C++? – curiousguy Aug 16 '12 at 17:22
  • @Abyx, *"i.e. interface can't have public destructor."*, Bjarne Stroustrup disagrees with you: [*"public virtual destructors is a tried and true technique"*](https://github.com/isocpp/CppCoreGuidelines/issues/524#issuecomment-183770068). – Gill Bates Aug 26 '19 at 15:23
-5

To emulate Java interface, you can use an ordinary base with only pure virtual functions.

You need to use virtual inheritance, otherwise you could get repeated inheritance: the same class can be a base class more than once in C++. This means that access of this base class would be ambiguous in this case.

C++ does not provide the exact equivalent of Java interface: in C++, overriding a virtual function can only be done in a derived class of the class with the virtual function declaration, whereas in Java, the overrider for a method in an interface can be declared in a base class.

[EXAMPLE:

struct B1 {
    void foo ();
};

struct B2 {
    virtual void foo () = 0;
};

struct D : B1, B2 {
    // no declaration of foo() here
};

D inherits too function declarations: B1::foo() and B2::foo().

B2::foo() is pure virtual, so D::B2::foo() is too; B1::foo() doesn't override B2::foo() because B2 isn't a derived class of B1.

Thus, D is an abstract class.

--end example]

Anyway, why would you emulate the arbitrary limits of Java in C++?

EDIT: added example for clarification.

curiousguy
  • 8,038
  • 2
  • 40
  • 58
  • I usually try to avoid multiple inheritance in C++, so I've never come across virtual inheritance before. Thanks for the clarification! – DaoWen Aug 14 '12 at 09:26
  • 1
    @DaoWen: please be advised, this answer is technically **INCORRECT**, and the poster has refused to fix it. – Cheers and hth. - Alf Aug 14 '12 at 10:30
  • 1
    @Cheers and hth. - Alf: could you please elaborate on why it's incorrect? – DaoWen Aug 14 '12 at 10:32
  • 5
    @DaoWen: The third paragraph has at least two interpretations, but both of them incorrect. In C++ you can inherit in an implementation of an interface, Java-style. That's what virtual inheritance is about in this context. The part that "curious guy" mentions in 2nd para is just indirectly relevant: it's part of the basis the allows the Java-like implementation inheritance, but it does not enable it. – Cheers and hth. - Alf Aug 14 '12 at 10:39
  • 1
    The notion of 'interfaces' in Java is not a 'limit'. It is a nice way of guaranteeing that a class can do X in situations where you can not provide a generic X function as the implementation depends too much on how a class works. – thecoshman Aug 14 '12 at 10:52
  • @thecoshman Java `interface` are arbitrarily castrated. There is no sound motive for not allowing method to provide an implementation. Abstract base classes in C++ do not have this restriction. – curiousguy Aug 14 '12 at 17:30
  • @Cheersandhth.-Alf Back to the subject matter: if my answer is ambiguous or unclear, I can try to clarify it. Please explain how the 2nd para is only indirectly relevant. How do you interpret the third paragraph? – curiousguy Aug 14 '12 at 17:39
  • @Cheersandhth.-Alf Can you back up your criticisms with facts? – curiousguy Nov 23 '14 at 12:53
  • @curiousguy: the statement "in C++, overriding a virtual function can only be done in a derived class of the class with the virtual function declaration, whereas in Java, the overrider for a method in an interface can be declared in a base class" is incorrect for C++. Example: (http://ideone.com/9t0vZo). – Cheers and hth. - Alf Nov 23 '14 at 20:16
  • @SO moderators: you've happened to delete the statements explaining what was wrong with the answer, leaving only the fallout discussion. – Cheers and hth. - Alf Nov 23 '14 at 20:19
  • @curiousguy: to help with googling the technique, it's known as "inheritance by dominance" (in a virtual inheritance hierarchy). the term is not used in the standard. but e.g. in visual c++ warning. – Cheers and hth. - Alf Nov 23 '14 at 20:20
  • @Cheersandhth.-Alf I have edited my answer for clarity. – curiousguy Nov 24 '14 at 20:31