Interface terminology terminology

If inheritance is an OO method for implementing an is-a relationship, and composition / aggregation implements a has-a relationship, what would be the name for the relationship using the Java / .NET style interface?

I really like the term can-do , since such interfaces are often used to indicate supported operations such as Sortable, Clonable, etc., and it sounds really positive. The only name that comes to my mind that I actually saw is that it implements , but in reality it does not describe so much.

+5
source share
3 answers

Probably the most important term to search for: Role .

The interface describes the Role .

This is why many interfaces have the suffix "capable": Cloneable , Serializable , Runnable , etc.

He likes to assign one or more roles.

+1
source

In most object-oriented programming languages, interfaces and classes both pass their relations through inheritance, and inheritance is an is-a relationship, regardless of whether it is an interface, class, abstract class, etc.

In fact, classes themselves are kind of interfaces, they just provide an implementation as well. There is not much difference between an abstract class without an implementation and an interface, although the implementation inside may have some differences. (the biggest of that in Java and .NET you cannot inherit several abstract classes)

So, from a conceptual point of view, is-a is inheritance. has-a is containment / composition / aggregation / regardless

The term "implements" is rather a subclass of "inheritance" or "subclasses". You cannot subclass an interface, but you can understand it. This is a more subtle detail, although it makes some sense.

Not all languages ​​have interfaces. C ++, for example, no. However, you can fake them with abstract classes without implementations, as I mentioned earlier, but since C ++ allows multiple inheritance, the problem is not such a big problem (as long as they are pure abstract classes without implementations, if you start mixing the code in them, then it becomes more hairy)

+1
source

is-a is also reasonable for interfaces. In Java, a ArrayList is-a List and a TreeMap is-a Map .

If you write a method that takes List as an argument:

 public void someMethod(List someList) { // ... } 

It is valid to pass an instance of ArrayList as an argument to this method, because ArrayList is a List .

0
source

Source: https://habr.com/ru/post/1200394/


All Articles