Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing the same method signature from two 'interfaces'

Using pure virtual methods for faux-interfaces in C++, what happens when a concrete class derives from two 'interfaces' which have an identical method declaration? e.g X sub-classes Iaaa and Ibbb and implements a method virtual void setVisible(bool);.

Does the fact Iaaa and Ibbb have no method body make things easier/better than a more classical diamond inheritance scenario, and lets X::setVisible be the implementation for both Iaaa::setVisible and Ibbb::setVisible?

I suppose a more C++ way of phrasing the question is "what happens when one class uses MI to derive from 2 classes which have identical signatures for a pure virtual method".

like image 866
Mr. Boy Avatar asked Oct 24 '11 08:10

Mr. Boy


People also ask

Can a class implement two interfaces with the same method signature?

No, its an errorIf two interfaces contain a method with the same signature but different return types, then it is impossible to implement both the interface simultaneously.

What if we have same methods in two interfaces?

A class implementation of a method takes precedence over a default method. So, if the class already has the same method as an Interface, then the default method from the implemented Interface does not take effect. However, if two interfaces implement the same default method, then there is a conflict.

What to do if a class implements two interfaces which coincidentally have one method with the same name and signature?

Question 2: What to do if a class implements two interfaces which coincidently have one method with the same name and signature? Implement both interfaces implicitly. Implement at least one interface explicitly. A class cannot implement multiple interfaces.

Can a class implement 2 interfaces?

Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class. By convention, the implements clause follows the extends clause, if there is one.


2 Answers

what happens when one class uses MI to derive from 2 classes which have identical signatures for a pure virtual method

The behavior is well defined. The derived class overriding method will constitute implementation of both the interfaces (i.e. abstract classes with all pure virtual functions) methods.

Also, 2 other points from your question

  1. diamond inheritance hasn't yet took place, because you haven't mentioned any common base for Iaaa and Ibbb
  2. You cannot implement, 2 X::setVisible(bool) with same signature
like image 151
iammilind Avatar answered Oct 01 '22 12:10

iammilind


It depends. If both functions have the same signature, and you want to replace both of them with the same function, then just do it; there is no problem, and your function will implement both of them. If they have different signatures, then you'll need two different functions to implement them. And if you want different implementations (likely the case if the interfaces are unrelated), but they have the same signature, then you'll need to introduce intermediate classes to "rename" them, e.g.:

class MaskSetVisibleInAaa : public Aaa
{
    virtual void setVisibleInAaa( bool ) = 0;
    virtual void setVisible( bool newStatus )
    {
        setVisibleInAaa( newStatus );
    }
};

class MaskSetVisibleInBbb : public Bbb
{
    virtual void setVisibleInBbb( bool ) = 0;
    virtual void setVisible( bool newStatus )
    {
        setVisibleInBbb( newStatus );
    }
};

class ConcreteImplementation
    : public MaskSetVisibleInAaa
    , public MaskSetVisibleInBbb
{
    virtual void setVisibleInAaa( bool );
    virtual void setVisibleInBbb( bool );
};

(I would also question your use of "faux-interface". C++ fully supports real interfaces—more so, in fact, than some other languages which have an interface keyword. An interface defines a contract; which, unless the language has special support for programming by contract, implies in most cases concrete code in the interface, with virtual functions being private.)

like image 30
James Kanze Avatar answered Oct 01 '22 13:10

James Kanze