Six steps before you reach for an abstract class
My students love abstract classes a little too much. This is how I talk them down, with a train.
I've seen it many times. A student spots two lines of code that look alike, gasps, and creates an abstract class.
I get it. We teach them DRY, “don't repeat yourself”, and they take it to heart. But I think repeating yourself twice is often fine, and a tangled family tree of classes is not. It's like buying a warehouse because you own two similar boxes.
There's a second problem. At university you often start from an empty file. At work you inherit somebody else's code, plus libraries you're not allowed to touch. We call it legacy code, which is a polite way of saying “good luck”.
So I came up with something for my students, and gave it a silly name: LOCO-MOTIVE, or Legacy-Oriented COde MOdernisation through Valuable Iterative Enhancements. Yes, that acronym is working overtime.
If you don't write code, the idea still holds: try the cheap thing first.
Every choice has a price
I sort design choices the way I sort takeaway options.
- Cheap: use something that already exists. Little risk, easy to look after.
- Moderate: make something new because nothing fits. Fine, but now it's yours to maintain.
- Expensive: build an abstract class that others depend on. Powerful, and painful to change later, because everything attached to it moves too.
The six steps

Start at the top, and only go down when you have to.
- Use what's there. Java already has
Collections.sort(). Your own sorting algorithm is very nice, and please delete it. - Tweak what's there. Override a method, like
toString(), without breaking what it promises to do. - Hold on to an object. Store it as an attribute. Careful here: if someone else can change it behind your back, they will. Keep your own copy of things like dates.
- Make a class. You need a
Loggerfor your project and nothing fits? Go for it. - Make an interface. Describe what something can do without saying how, like a
PaymentProcessorfor different ways to pay. This is “composition over inheritance”, if you want the fancy term. - Make an abstract class. It has its place, like
ShapeforCircleandRectangle. But you walked past five other doors to get here, so I hope you knocked.
The part people miss is the arrow back to the top. Made a new class in step 4? Go back to step 1 and ask whether you can now reuse it. Each lap is a chance to keep things simple, and to find mistakes while they're small. I reckon this matters when you work with old code, where small safe improvements beat a heroic rewrite.
Yes, draw the boxes
I know, diagrams. Some of you think they're a waste of time. I sketch mine by hand, because it's quick. Bad design looks bad on paper long before it hurts in code.
You only need four arrows:

If your sketch looks like a plate of spaghetti, so does your code.
Before you quote me
This comes from my own teaching and projects, not from a study. My “cheap” might be your “moderate”, and I think arguing about that with your team is half the value. If you write functional code, much of this won't apply, and you're probably feeling smug right now.
But next time your fingers start typing abstract, stop and go through the other five first. If you still need it, you'll know why.