Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly extend interface?

Tags:

c++

I have interface based on another:

class IDrawable {
public:
   virtual ~IDrawable();
};

class IExtendedDrawable: public IDrawable {
public:
   virtual ~IExtendedDrawable();
};

class DrawableImplementation: public IDrawable {
public:
   virtual ~DrawableImplementation();
};

class ExtendedDrawableImplementation: 
   public DrawableImplementation, public IExtendedDrawable
{
public:
   virtual ~ExtendedDrawableImplementation();
};

Then ExtendedDrawableImplementation = DrawableImplementation (+IDrawable) + IExtendedDrawable (+IDrawable)

Is it right to have IDrawable twice in same class?

like image 876
kravemir Avatar asked Aug 21 '12 19:08

kravemir


People also ask

How do you extend an interface?

An interface can extend another interface in the same way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.

Can we Extending interface?

Yes, we can do it. An interface can extend multiple interfaces in Java.

What happens when you extend an interface?

An interface is a contract without implementations (Java8 introduced default methods). By extending you extend the contract with new "names" to be implemented by concrete class.

How do you extend an interface in TypeScript?

Extending Interfaces in TypeScript # Use the extends keyword to extend interfaces in TypeScript, e.g. interface Dog extends Animal {age: number;} . The extends keyword allows us to copy the members from other named types and add new members to the final, more generic interface. Copied!


2 Answers

I'll give the benefit of the doubt that you DO indeed need/want multiple inheritance. I see it as good in only limited situations, and interfaces is one of them (even Java allows this).

As said above, use virtual inheritance and be sure to only use pure virtual methods in the interface classes.

class IDrawable {
public:
   virtual ~IDrawable();
   virtual void doSomething() = 0;
};

class IExtendedDrawable: virtual public IDrawable {
public:
   virtual ~IExtendedDrawable();
   virtual void doSomethingElse() = 0;
};

class DrawableImplementation: virtual public IDrawable {
public:
   virtual ~DrawableImplementation();
   virtual void doSomething() {/*code here*/}
};

class ExtendedDrawableImplementation: 
   public DrawableImplementation, public IExtendedDrawable
{
public:
   virtual ~ExtendedDrawableImplementation();
   virtual void doSomething() {/*code here*/}
   virtual void doSomethingElse() {/*code here*/}
};
like image 56
MartyE Avatar answered Oct 18 '22 02:10

MartyE


It largely depends on the logic inside the classes is, but IMO you're better off using composition instead:

class ExtendedDrawableImplementation : public IExtendedDrawable
{
   IDrawable* drawableImplementation; //points to a DrawableImplementation
public:
   virtual ~ExtendedDrawableImplementation();
};

Multiple inheritance is rarely the answer.

like image 29
Luchian Grigore Avatar answered Oct 18 '22 01:10

Luchian Grigore