Download

Interfaces inheriting from other interfaces

Hey there. Just thought I would share something I stumbled upon while messing with some code, trying to break things, who knows what I was thinking lol.

I wasn’t able to find any documentation or wiki entries mentioning but you can actually create interfaces that inherit from other interfaces. I don’t know if this is intended or something that just happens to slip past the compiler but it’s been compiling for me and works fine so far.

The trick is that if I want IMyInterface_2 to inherit IMyInterface_1, I would inherit IMyInterface_1 in UMyInterface_2 (not IMyInterface_2).

IMyInterface_2 I also didn’t have to override the virtual functions from IMyInterface_1 and when a class inherits from #2 the compiler enforces the requirements of both interfaces.

Again, I have no idea if this is supposed to be allowed or not but I haven’t come across any problems yet.

Thought this was a fun find and figured I’d share in case this is of use to anyone.

That is very interesting! Thanks for sharing your fun findings!

Great to hear from you!

:slight_smile:

Rama

I thank you for finding out that this is doable, but what you said about inheritance is horribly misleading. In order to make it work, UMyInterface_2 needs to inherit from UMyInterface_1 and IMyInterface_2 needs to inherit from IMyInterface_1. The first (U) only tells UE4 that one derives from the other; the second (I) tells the compiler. Without it, I wasn’t able to override the functions of the first interface at all (which defeats the purpose of interface inheritance).

Is this still possible? I tried this and it fails to compile:

No matter what I do, it says missing { in class on the line I call out.
UHT might not like me trying to add any extra items after the UInterface inheritance keyword.



UINTERFACE(BlueprintType)
class UWRAIAttackStateInterface : public UInterface, public UWRAIStateInterface           //<<-----Compiler error on this line.
{
	GENERATED_UINTERFACE_BODY()
};

class WRAITH_API IWRAIAttackStateInterface : public IWRAIStateInterface
{
	GENERATED_IINTERFACE_BODY()

	//Methods go here
};


It’s not the end of the world if this isn’t possible but I’ll just have to make everything inherit from both interfaces.

Have you tried doing this?

I do this on 4.15 and works just fine.

@illYay This works for me in 4.11
UnrealHeaderTool probably doesn’t like that you’re trying to inherit from multiple U-classes (which isn’t even necessary since UWRAIStateInterface should already inherit from UInterface anyway).


UINTERFACE(BlueprintType, Blueprintable)
class YOUR_API UBaseInterface : public UInterface
{
	GENERATED_UINTERFACE_BODY()

};

class YOUR_API IBaseInterface
{
	GENERATED_BODY()

public:

	// methods

};


UINTERFACE(BlueprintType, Blueprintable)
class YOUR_API UDerivedInterface : public UBaseInterface 
{
	GENERATED_UINTERFACE_BODY()

};

class YOUR_API IDerivedInterface : public IBaseInterface
{
	GENERATED_BODY()

public:

	// methods

};