1

Is there a time when it's more appropriate to subclass AbstractBorder than to implement the Border interface when developing a custom Swing border? And vice versa?

From the example code that I've seen, it seems quite arbitrary.

Moonbeam
  • 2,283
  • 1
  • 15
  • 17

2 Answers2

1

For clean OOP you should implement the Border interface. But, if you do not to plan to extend any more your new Border class with new classes that inherit from your Border, it is more convient to extend AbstractBorder.

AbstractBorder makes part of Swing to make your work easier. So use it, but do not make it the base of a class hierarchy.

PeterMmm
  • 24,152
  • 13
  • 73
  • 111
0

Sometimes when Java has an interface XYZ and a class AbstractXYZ the class does quite a bit of work and leaves you just the bare minimum to implement (e.g List and AbstractList). Sometimes the abstract class just provides a do-nothing implementation (e.g. MouseInputListener and MouseInputAdapter). AbstractBorder is more of the latter case; it doesn't really help the implementation of Border but it does provide some helper methods which (AFAICS) are of more use to the component 'hosting' the border than the border itself. So, I would say it probably is a bit arbitary which one is used. I'd recommend using AbstractBorder anyway, there's no reason not to (unless you need to derive from another class of course.)

@PeterMmm, why shouldn't you use it as part of a hierarchy? A lot (All?) of the standard Swing borders derive from AbstractBorder.

Peter Hull
  • 6,683
  • 4
  • 39
  • 48