Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What you are describing is an interface, not inheritance. They are implementing an pulley interface, not inheriting a specific pulley implementation from a common housing or something.


The distinction between "implementing" an interface and "inheriting" a base class is largely just a Java-ism.

To some extent, it's a distinction without a difference - at a semantic level, Java only distinguishes between interfaces and purely abstract base classes because one allows multiple inheritance and the other doesn't, and the separate "inherits" and "implements" keywords flow from there. Other languages (like C# and Kotlin) distinguish between interfaces and overridable classes for the purposes of deciding when to allow multiple inheritance, but use the same syntax and terminology for inheriting from both interfaces and base classes. Still other languages make no structural distinction, but still use the word "interface" to refer to base classes that don't include any implementation, and therefore aren't so susceptible to the diamond inheritance problem. TBH, I think the Java-style treatment is the least conceptually consistent, since it's entirely possible to "inherit" a base class without inheriting a single bit of information, either because everything it declares is abstract or because everything got overridden.

There's a pretty decent coverage of all the grotty details of this concept space, and how different languages treat it, in the beginning of the gang of four book.


I kind of like interfaces as they are basically less powerful traits. I think there is a big difference to class inheritance and the difference between e.g. Jave and C# is just syntactic.

When you implement interfaces you have to explicitly implement all methods. If you implement them correctly all methods using them continue to stay correct (including C# extension methods or java interface default methods).

With class inheritance you have two large problems: you also inherit state that you might not need, might not have access too and might change in future versions of your base class.

Secondly you might need to override methods of the base class to provide correctness. If your base class adds/changes methods your subclass can easily behave wrongly.


>it's a distinction without a difference

Never looked at it that way. With inheritance, you're essentially re-using behavior that treats like things with like properties in a similar way. With interfaces, you are providing unique, black-boxed behavior that conforms to an operational specification.

Interfaces are also more externalized. They are intended for consumption from outside of any particular class hierarchy, and don't necessarily represent like things nor operate on the internal properties of a particular class or hierarchy.

OTOH, consider the use of inheritance wherein a major operation or algo is implemented and exposed via a public method on an abstract base class, but one or more type-specific abstract methods on which the algo depends is implemented by subclasses in a type-specific way. This more "inverted" structure probably better highlights a distinction with a real difference. Your subclasses are inheriting major behaviors and "tweaking" the behavior as needed.

You could probably pull this off with interfaces, but it's more convoluted. You'd have one class with several external classes that implement some interface and have reduced access to properties of the original class. So, you may now have to expose properties that you'd otherwise prefer not to. And, you have to manage instantiation of the appropriate interface-implementing classes.

>it's entirely possible to "inherit" a base class without inheriting a single bit of information

Sure, you can override every method in a base class, essentially rendering it not inheritance. But, you could also not extend anything at all and opt not to use interfaces or any other OO-construct. It comes down to design.


> Java only distinguishes between interfaces and purely abstract base classes because one allows multiple inheritance and the other doesn't

No. This is what former C/C++ programmers coming to Java typically believe (some of them even after spending 10 years coding in Java, because old habits die hard).

interface = interface

abstract base class = implementation

Interface and implementation may seem like "a distinction without a difference", because one is just a list of method headers and the other is a list of method headers with method bodies, but semantically they are the opposite of each other.


Thank you. I came up in C++ land where the distinction between interfaces, abstract classes, and base classes is pretty muddy.


Interfaces are a distinct concept from inheritance. Inheritance can be used as an interface mechanism, but that's merely an implementation detail. For example, Go has interfaces with no inheritance at all. It's unfortunate that most people think of Java or C# interfaces by default; it really creates a lot of confusion.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: