• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Interface V/S Abstract class  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a design dilemma about the usage of interface vs abstract class. I have a set of classes which will have almost similar functionality and method signatures. Some methods in these classes are generic and hence can be implemented in a super class whereas others are very specific to the subclass.
1. Is it appropriate to just use an abstract class as superclass and then let other classes extend from it. Or
2. should I first write an interface defining the methods and put an abstract class that implements some of the methods from this interface and leave rest of the method implementation to the subclasses.
Please let me know the relative pros and cons of the 2 approaches and which is more appropriate given the circumstances
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

In my opinion, it is better to create an interface, then an abstract class implementing the interface, and then the individual classes that extend the abstract class. This will give you the benifit of flexibility & extensibility in your application.

If in the future, you need to add another abstract class providing a seperate implementation set, it will be easier to implement within your eisting framework. It will also provide the added flexibility of having classes that can provide their own implementations of interface methods if they do not need to extend the abstract class.

Directly using abstract classes will also serve your purpose. You just need to evaluate how much flexibility do you need, and then decide on using either abstract classes or interfaces.

Hope this helps.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!