Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

In terms of rework what to choose an interface or abstract class ????

 
simon paul
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose i have an application w/c may be designed by abstract classes also
by interfaces.I would like to go for interfaces for dynamic plug in features.Now if in mid of development lot of changes come in business logics
and fn() to be added diff places how to lesses the rework if interface i choose and it gets changed in so many places.Any work around good Disign patterns or alternative approach / go for <<abs>> could any on guide a bit.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prefer interfaces over abstract classes, that's the best rule I've seen on the subject.
In other words, if you are choosing between the two and see no obviouc benefits either way, use an interface; chances are you'll be glad you did.
There are two basic reasons for using an abstract class instead of an interface. One is to preserve code in a method implementation which you are quite sure will be used by most, if not all, classes that will extend from this abstract class. Furthermore, you see no likely benefit from reserving inheritance of another class somewhere down the road.
Two, an abstract class can contain a sequence of important actions, expressed as method calls in a prescribed order. Interfaces can't do this at all, so if this feature is required -- in applying the Template design pattern, for example -- an interface isn't really an option.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, there's one pther advantage that abstract classes have over interfaces, specifically when it comes to "rework." I wonder if Simon isn't already aware of this one, having asked the question.
If you've got a library API meant for users to implement, as he describes, then if that API is described by an interface, then adding a method to the interface breaks all the user code. The user implementations will be missing a method and calling it will give an AbstractMethodError.
If, instead, that library had used an abstract class, then a reasonable default implementation could have been provided. Josh Bloch talks about this in "Effective Java."
Anyway, except for this, I agree with Michael: interfaces are generally to be preferred.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been referencing this article all over the place lately: Why Extends Is Evil. Of course the title is an exageration, but the point is good and some of the examples are illuminating.
 
Message for you sir! I think it is a tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!