I was wondering if empty overridden methods inherited from an abstract interface class are a sign of bad interface design. Sometimes interfaces contain methods that are useful only partially for all the possible implemention classes; in the classes where unused methods are not necessary, they are just left empty.
In the following example, connect() and disconnect() are common to the two implementation classes IpConnection and SmtpConnection. However, prepare() is not necessary in SmtpConnection and is left empty.
In this kind of situations, is it better to remove prepare() from the abstract interface IConnection and call it explicitly, or leave it empty? What if empty (unused) methods grow more and more?
#include <memory>
class IConnection
{
public:
virtual void connect() = 0; /* common */
virtual void disconnect() = 0; /* common */
virtual void prepare() = 0; /* partial */
};
class IpConnection : public IConnection
{
public:
void connect() override { /* ... */ }
void disconnect() override { /* ... */ }
void prepare() override { /* ... */ }
};
class SmtpConnection : public IConnection
{
public:
void connect() override { /* ... */ }
void disconnect() override { /* ... */ }
void prepare() override { } /* empty */
};
int main()
{
std::unique_ptr<IConnection> connection;
connection = std::make_unique<SmtpConnection>();
connection->connect();
connection->prepare();
connection->disconnect();
}