0
$\begingroup$

In Python, we have metaclass and abstract class.

In Java and C#, we have interface and abstract class.

The relation between a metaclass and a class is instantiation, so it extends the instantiation relation between a class and an object.

The relation between an abstract class and a class is inheritance, so it extends the inheritance relation between concrete classes.

The relation between an interface and a class is implementation.

  • Is this implementation relation an instantiation relation, an inheritance relation, or neither?

  • If neither, can this relation be viewed as some extension of some relation between some classes or objects?

  • is an interface more like a metaclass or an abstract class?

Thanks.

$\endgroup$
2
$\begingroup$

From notes I have from university, here is an explanation I found useful that may help

An interface, or protocol as it is sometimes called, is a device that is used to allow unrelated objects to interact with one another, by implementing an agreed upon system of behavior. When a class implements an interface, the class agrees to implement all of the methods defined in the interface. Interfaces are useful since they capture similiarity between unrelated objects without forcing a class relationship.

The important line is the bottom, there is no class relationship between classes that impliment an interface. You cant view it in the same way you would with instances of classes and abstract classes. These give a direct relation in terms of object type and subtype. It is not a guarantee of relation between objects but instead a guarantee of functionality.

To your bullets I would say; Neither, not really, and it is not really like either metaclass or abstract class at all.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ “Implement all the methods in the interface” is not true for Objective-C. The caller will check which optional methods are implemented and adapt its behaviour. $\endgroup$ – gnasher729 Jan 3 '18 at 18:37
  • $\begingroup$ @gnasher729 Objective-C interfaces are a whole different ball game, the question here was Java and C#. Objective-C protocols are Java interfaces $\endgroup$ – lPlant Jan 3 '18 at 19:53
1
$\begingroup$

Is this implementation relation an instantiation relation, an inheritance relation, or neither?

I would say interfaces are very close to abstract classes. They provide the functionality of multiple inheritance in single-inheritance languages.

A class X implementing an interface Y, or inheriting from another class Y, both allow me to treat objects of class X as Ys in my code. I rarely create an abstract class, but when I do, I always wonder whether to make it an interface instead.

In Java and C#, classes can be marked as abstract to indicate they cannot be instantiated. It would have made perfect sense to specify interfaces as bodiless classes to indicate they cannot have method bodies or other executable code.

So an implementation relation is effectively a (multiple) inheritance relation, except that the language designers happened to choose different terminology.

Instantiation is the relationship between classes and their objects. in statically typed, compiled, strictly class-based languages such as Java or C#, instantiation is very different from inheritance and implementation: the latter are compile-time relationships defining which code will be associated with which objects at runtime, while instantiation is something that happens at run time, defining a relationship between an entity existing only at run time (an object) and something defined at compile time, namely the code associated with it, specified in the class(es) it belongs to.

This distinction is blurred by reflection, which can create all of these things at run time. It is also much blurrier in dynamic languages such as Python, in which none of these things are defined at compile time in the first place. Another way to blur it is to allow method implementations and inheritance to happen not only on classes, but also on individual objects, such as in a Lisp dialect I've used. If an object can define its own methods, it becomes more like a class, so instantiation becomes more like inheritance.

Is an interface more like a metaclass or an abstract class?

Metaclasses don't really have a pendant in statically classed languages such as Java or C#. They allow code to be defined that is executed at class initialization time. Java and C# also allow this, but they don't offer a construct that allows such code to be bundled together like that. Abstract classes can be used for this purpose, so I would say they're the closest match.

| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.