News for hackers. Stuff that matters.
Re:Inheritance Often Doesn't Make Sense by DubiousPusher on Saturday March 17, 2018 @02:53PM (#16608429)
If you ever open the hood of a modern car and look around a bit you'll discover that at the front there's a half dozen pullies connected to one or more belts. Each of these pullies is connected to some diffeerent system each able to turn rotational acceleration into a useful service. The alternator converts that rotational energy into electricity. The AC unit uses it to operate a heat pump. The coolant and oil pumps use it to circulate fluid. In the past, these parts were sometimes driven by another system or integrated directly into another part. But over time, as a convenience to many different parties it was decided they should all implement a pully and connect dirctly to one of the main belts that connect directly to the motor. Not a single one of these parts has anything you'd call a taxonomical relationship. They do have an invented one. And it is as much about conveniencing the overall system design as much as conveniencing the individual part design. They are all "pully implementers" subscribed to the "belt druve" system.

This is the inheritence I find most powerful in software. These things aren't the same but if we treat them the same, the downstream and upstream code are simpler, easier to replace and consistent. I don't worry too much about the philisophical relationship between the objects.

Re:Inheritance Often Doesn't Make Sense by dyarosla on Saturday March 17, 2018 @03:34PM (#16608639)
You’ve described interfaces, not so much inheritance.
Re:Inheritance Often Doesn't Make Sense by kbsletten on Saturday March 17, 2018 @04:24PM (#16608882)
In the language of the article, this is subtyping without subclassing. Sure Java/C# implement this as interfaces, but in C++ this would be a case of multiple inheritance. This is a good example of the article's main point that people mean a lot of different things by inheritance and get different value from the different applications of the concept.
Re:Inheritance Often Doesn't Make Sense by humanrebar on Saturday March 17, 2018 @10:56PM (#16610283)
There are at least a few ways to define and implement interfaces in C++ without touching inheritance.
Re:Inheritance Often Doesn't Make Sense by andrewflnr on Saturday March 17, 2018 @11:37PM (#16610374)
Structures of function pointers and, what, string method names? My C++ is rusty, but that's all I can think of. I guess there's a lot of variations on structures of pointers...
Re:Inheritance Often Doesn't Make Sense by nostrademons on Sunday March 18, 2018 @02:13AM (#16610669)
Templates and compile-time polymorphism. This is what the STL is based upon. A forward iterator is any type for which the * and ++ operators are defined; you can use it in any template that makes use of just those syntactic forms, but will get a compiler error if you try to instantiate a template with a type that does not support those operators.

Punning on memory layouts, a la PyObject_HEAD or some of the objects in V8. With this approach, you make sure that all related types have the same memory layout for a subset of fields at the beginning of the struct. Then, a reinterpret_cast on a pointer to one member of the type will yield the same values as a different member of the type, and so can be used generically by code that depends only upon those members. (You have to be very careful with this approach, because multiple inheritance and vtables will usually break it, but if you absolutely positively must have zero-overhead runtime polymorphism it's occasionally the best option.)

#include and preprocessor magic to redefine symbols based on preprocessor defines. Used to be the old way to swap out different implementations of a cross-platform interface, but seems to have largely fallen out of favor in the last couple decades.

Linker magic to accomplish the same, where the header file defines a common interface but the linker may link with one of several different object files to implement that interface at link time. This one is still fairly commonly used; Google and Facebook both have their own custom STL & libstdc++ implementations, and this is also commonly used to swap out eg. malloc implementations.

dlopen & dlsym to do this at runtime. Header file defines the interface, dlsym gives you back a function pointer or set thereof for a plugin implementation.

Re:Inheritance Often Doesn't Make Sense by stephen_gareth on Sunday March 18, 2018 @03:59PM (#16613943)
In Sean Parent's 'Inheritance is the base class of evil' talk Sean outlines an interesting way to achieve runtime polymorphism without inheriting interfaces in C++.

https://www.youtube.com/watch?v=bIhUE5uUFOA

Re:Inheritance Often Doesn't Make Sense by Paul_Dirac on Monday March 19, 2018 @07:24AM (#16617596)
But he uses interface inheritance.

You see it at 9:44. Where he creates the concept_t and int_model_t inherits from it.

Re:Inheritance Often Doesn't Make Sense by stephen_gareth on Wednesday March 21, 2018 @03:50PM (#16641674)
Indeed. But the original type isn't 'burdened' with the interface. I've used the technique for a simple audio signal processing chain.
Re:Inheritance Often Doesn't Make Sense by jayshua on Saturday March 17, 2018 @11:38PM (#16610376)
Could you name a few? I was under the impression that inheritance was the only way.
Re:Inheritance Often Doesn't Make Sense by dwaltrip on Saturday March 17, 2018 @09:31PM (#16610054)
Can you give a mechanical example of inheritance, so we can see the difference?
Re:Inheritance Often Doesn't Make Sense by baddox on Saturday March 17, 2018 @10:04PM (#16610126)
I think the term “interface” is used in many related but different ways in software, but I think the most obvious mechanical analogy would be basically anything that uses a common “plug” or “adapter.” Like a vacuum cleaner with different attachments. Or even HDMI for video/audio.
Re:Inheritance Often Doesn't Make Sense by weberc2 on Sunday March 18, 2018 @12:54AM (#16610500)
You're confusing inheritance with implementing an interface. I'm not sure there is an example in the real world, because the real world is concerned with function, not taxonomy. It hardly makes sense to say "cordless drill inherits from screw driver"; rather, they both implement the "drives screw" interface. Ontological inheritance really doesn't make any sense at all for modeling systems (sometimes ontologies are useful as data in a system, but probably never for modeling the system itself).
Re:Inheritance Often Doesn't Make Sense by baddox on Sunday March 18, 2018 @01:06AM (#16610527)
I’m not confusing those things. I’m giving an example of an interface, as requested.
Re:Inheritance Often Doesn't Make Sense by mcbits on Sunday March 18, 2018 @02:49AM (#16610744)
They wanted an example of inheritance. Something like:

You need an office. You could create a new office from scratch, or you could also just put a desk in a room in your house and give it a unique address like "Suite 1". Now it's both a Room and a HomeOffice.

But if your HomeOffice exposes a method BuildWall(), which mutates the office into a collection of two Rooms, then it would stop fitting the definition of a Room after calling that method.

I don't know if there are different tax consequences for one-room vs. multi-room home offices, but that's one area where the distinction could at least conceivably be significant enough to affect whether you'd prefer to build a separate office or inherit the old room, or maybe just rent an existing office from a third party.

Re:Inheritance Often Doesn't Make Sense by dllthomas on Sunday March 18, 2018 @02:20AM (#16610683)
It may have been edited, but at least presently the comment you replied to asks for an example of inheritance.
Re:Inheritance Often Doesn't Make Sense by baddox on Sunday March 18, 2018 @02:43AM (#16610730)
Oh rats, my mistake. It is harder to come up with s good example of mechanical inheritance. Something like the cassette tape adapter kinda feels like inheritance, in the sense that it can be substituted in for a normal cassette tape but provides its own unique functionality.
Re:Inheritance Often Doesn't Make Sense by jwdunne on Sunday March 18, 2018 @05:49AM (#16611167)
Yes, I'm struggling to think of a good mechanical example of inheritance as code reuse.

But I don't agree with the "that's an interface, not inheritance" argument. Well designed super classes tend to provide an interface - they just don't use the "interface" keyword.

If you declare a method as abstract, you're defining an interface that subclasses use to define new behaviour. You see this all the time. If a method is defined as protected, you're saying "I'm allowing subclasses to use this interface to change behaviour".

For example, test frameworks usually provide an overridable method that's run before each test - setUp. That's interface - "I will call setUp before every test - feel free to override it".

To me, that's where shit gets hard. It has to have a well defined interface, it has to be an is-a relationship and it has to be a subtype (Liskov). That's why we see composition over inheritance because it's far simpler to just drop one of the constraints and get decent work done.

Re:Inheritance Often Doesn't Make Sense by sametmax on Monday March 19, 2018 @03:56AM (#16616736)
Inheritance does allow you to avoid implementing part of the interface.
Re:Inheritance Often Doesn't Make Sense by eikenberry on Saturday March 17, 2018 @03:37PM (#16608653)
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.
Re:Inheritance Often Doesn't Make Sense by bunderbunder on Saturday March 17, 2018 @07:31PM (#16609654)
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.

Re:Inheritance Often Doesn't Make Sense by legulere on Saturday March 17, 2018 @08:47PM (#16609929)
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.

Re:Inheritance Often Doesn't Make Sense by unclebucknasty on Saturday March 17, 2018 @09:36PM (#16610065)
>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.

Re:Inheritance Often Doesn't Make Sense by Viliam1234 on Monday March 19, 2018 @04:36PM (#16622876)
> 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.

Re:Inheritance Often Doesn't Make Sense by DubiousPusher on Saturday March 17, 2018 @07:43PM (#16609701)
Thank you. I came up in C++ land where the distinction between interfaces, abstract classes, and base classes is pretty muddy.
Re:Inheritance Often Doesn't Make Sense by weberc2 on Sunday March 18, 2018 @01:04AM (#16610519)
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.
Re:Inheritance Often Doesn't Make Sense by donatj on Saturday March 17, 2018 @04:38PM (#16608947)
Which is also why these modern cars have turned a minor belt failure into an expensive catastrophic failure, often destroying much of the engine.

I'd hardly call it a design to aspire to.

I'd probably compare it more closely with a system where everything receives a mutable God object.

Re:Inheritance Often Doesn't Make Sense by guiscreenshots on Saturday March 17, 2018 @05:31PM (#16609165)
He's describing the accessory belt. You're referring to the timing belt.
Re:Inheritance Often Doesn't Make Sense by DubiousPusher on Saturday March 17, 2018 @07:40PM (#16609695)
Yes, the timing belt or timing chain is inside the engine casing and it can be bad when it or subordinate parts fail. Though a timing belt/chain failure causing a catastrophic event for the engine itself, I don't think that's very common. What I'm referring to is called the serpentine belt and is often accompanied by what's called an accessory or AC belt.
Re:Inheritance Often Doesn't Make Sense by LifeLiverTransp on Saturday March 17, 2018 @06:43PM (#16609433)
This description and definition discussion describes very well the definition lawyering and specsheet-rewritting that agile avoids, by having the walking specsheet (customer) sitting in during the creation process.
Re:Inheritance Often Doesn't Make Sense by sametmax on Monday March 19, 2018 @03:57AM (#16616746)
But modern cars don't need nearly as much maintenance and are more reliable on averable.
Re:Inheritance Often Doesn't Make Sense by donatj on Monday March 19, 2018 @12:57PM (#16620050)
That has not been my experience at all.

My 91 pickup and 99 minivan's engines are still going strong with well over 200k miles, rarely having anything major wrong with them in the entire time I own them, whereas my 2004 and 2009 cars both with ~100k miles have been nothing but total maintenance nightmares. Timing issues, computer failures. Sheer nightmares.