
Java
Interface inheritance is basically the same thing as multiple inheritance. In interface inheritance, the interfaces are classes that cannot have method implementations and variables. They may only have function prototypes. You are only allowed to inherit from one full blown class as a child in the interface inheritance model.
However, in multiple inheritance (which C++ supports), you're allowed to inherit from as many full classes as you want. This allows you to make those full blown classes as slim as a bunch of pure virtual functions (interface) or as full classes with method implementations and variables. For example, to implement something similar to a comparable interface in C++:
class Comparable
{
public:
virtual int compareTo(void* x) = 0;
};
class Foobar: public Comparable
{
public:
virtual int compareTo(void* x) { /*compare implementation here */ }
//rest of class
};
That would be pretty much the same thing as interface inheritance in Java. Any function that takes a Comparable pointer or reference will also be able to take Foobar. In C, you'd have to use function pointers to achieve the same effect. Basically in your structure, you'd have to have a pseudo v-table... a bunch of function pointers:
struct Comparable
{
int (*compareTo)(void* x) = 0;
};
struct Foobar
{
int (*compareTo)(void* x);
//rest of structure
int i;
int j;
int k;
};
void InitFoobar(struct Foobar* this)
{
this->compareTo = /*compare to function*/;
//rest of the initialization
this->i = 0;
this->j = 1;
this->k = 2;
}
If you want to pass Foobar into a function that takes a Comparable pointer, just cast the Foobar pointer to a Comparable pointer. The C standard guarantees structures Comparable and Foobar will be laid out exactly the same as long as the variables are declared in the same order. So the first 4 bytes, the function pointer, will be at the exact same offsets in both Comparable and Foobar. However, after the first 4 bytes, Foobar will have extra 12 bytes of stuff where Comparable will not. This memory arrangement lets you treat Foobar exactly like a Comparable. This is also how single inheritance is normally implemented in most languages.
Copyright © 2026 eLLeNow.com All Rights Reserved.