• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface via abstract class?  RSS feed

 
B Hayes
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've read a few articles re: interface vs abstract classes.
However, it seems to me that in time, core code inevitably changes (whether it be in 6 months or 6 years). A concrete class will implement changes that can optionally be overriden. An abstract class can either "pass the buck" (ie: declare new methods as abstract), or provide basic implementation that can be overriden without breaking code. An interface, on the other hand, demands that its new methods be implemented.
So, for example, assume that an interface (let's say HasPhysics) could change if we discover the 5th dimension. Suddenly all these classes implementing HasPhysics break (ie: must be changed) when new functionality is added.
I'm just curious: does it make sense in an environment that won't be discovering the 5th dimension any time soon (ie: most business environments) to "bundle-up" interfaces with a sensible abstract class that then has the option of breaking subclass code (ie: passing the buck via abstract methods), or not breaking it (ie: providing basic if any implementation that can be overriden)?
I guess the corollary to that is: are airtight base-class designs possible to bypass this problem? And if so, how?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think providing an "adapter" class that interface-implementors can use, or not, is a great idea; the java.awt.event.XXXAdapter classes, for example. But the possibility of needing to introduce new methods in an interface doesn't, I think, outweigh the alternate problem of having to carry along the baggage of a base class you don't need. Being able to implement an interface without extending a class gives you wonderful flexibility that shouldn't be sacrificed due to this particular concern.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an aside, please notice that those "adapter" classes don't have anything in common with the Adapter design pattern.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!