• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Abstract class vs Interface

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I always confuse in using Interface or Abstract class.
In my project, I have a Service like implementaions,Which can have one or more specific implementations.But I confuse in using Interface or Abstract Class for this purpose. Ofcourse all of the Service implemenations has some common features...
Till today, I have aroused with these types of questions so many times.
Can any one put me on right track.Thx in advance.
And also one very small question,What is the difference btn "Abstract Interafce" and an "Interface".In my view both are same.am I right?
Regards
Jelda
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use both!!!.
If you have several clases that have common futures and behavior you can use an abstract class to define all the common methods and leave as abstract all those that are specific to each service or could not have a default behavior. And plus if this application will grow with the time or could be used by someone else with out the need of your soruce code you could make interfaces that define and agreement between your system and it's service provider. Of course your abstract classes can implement this interfaces.
But remember to keep the things as simple as posible, and to have very well defined your interfaces before start because if you have several classes implementing and interface and you NEED to add a new method to this interface you'll broke the existing clases.
Anyway; I think as start you can use a simple concreate class ( default service ) and then refine it with subclasses for new services.
Inheritance is not to avoid coding but for specializing the behavior of an existing class. And interfaces are for make contracts, or agreement between diferent parts of a system, and allow maximun flexibility ( just think in the JDBC API, most of them are interfaces )

I hope this help you.
Regards
Zkr Ryz
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interfaces or abstract classes (either one) are key to most of the design patterns we all talk about, and those are mostly about decoupling classes and managing dependencies. Plugging myself shamelessly, here's a presentation I just did on a couple aspects of this: http://www.surfscranton.com/architecture/DependencyInversion.htm
They each have their good points:
Interface: Classes can implement multiple interfaces. For my money, this makes interfaces preferred unless there is some other compelling thing going on. But they don't have any behavior; you have to provide it all.
Abstract: An abstract class can contain code. You can build an abstract "framework" with partial functionality and developers can override certain methods to customize the behavior. I picked up calling these methods "developer hooks" from some PowerBuilder library. But you can only extend one abstract class at a time.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, the only reason why there are interfaces in Java is that it doesn't have multiple implementation inheritance. If it had, an interface would be equivalent to a fully abstract class. As you can't inherit from several classes at the same time, we do have interface to at least have multiple interface inheritance.
And to your other question: Yes, interfaces are always abstract. Adding an explicite abstract modifier to them does exactly nothing.
 
R Jelda
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for your all explanations.I am really familiar with Java and UML designing. But still I ever see some question marks in my mind when dealing with Interface/Abstract Class/Class.
Here I give you one of the situation where I got confused .
I have an Identifier class. It is like an id of an object.
In my project I would have 2-3 different implementations of this identifiers.(and all of the methods in it need to have some default implementaions...)
so..
here are confusions in designing..
1) Define a Identifier class, implement default implementations for all methods..And extend this class for the different Identifier and override the methods needed.i.e id2 extends id.And use Identifer class as a reference..
2)Define AbstractIdentifier class,which has some abstract methods and some common method implementaitons..And the remanining as usual..
3) or Define Identifier as an Interface.And implement it as needed in different identifier classes.And use Identifier interface as a refernce to other..
still has not found the reason to use Interface over abstract class or Class.I could n't see any advantages of the above one design over other.
Would be thankful for your comments..
Regards,
Jelda
 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use Indentifier as class(concrete) if you are in a position to define default implementations to the methods you wish to have in the class and if an instance of this class can have meaningful existance.i.e. if Identifier ii=new Identifier(); if ii can be object with acceptable state and beviour.
Use Indentifier as an abstract class if you donot have default implementations for all the methods and default state.Making a class abstract will prevent the user from creating instances.You can therefore ensure that the user subclasses your Indentifier and provides implementation to abstract methods before he creates instances.
By making your Identifier an abstract class u are saying I know all methods that the Indenfier should provide but at this point of time i have only enough information to define to implementation for some methods.
Use Indentifier as an interface if your are only sure of what methods concrete instances should offer its user.
By making your Identifier an interface u are saying I know all methods that the Indenfier should provide but at this point of time i donot have(or choose not to) enough information to implement those methods.
Hope that helps
Others please comment
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a neat Allen Holub article with the inflamatory title Why Extends Is Evil. That's an exaggeration of course, but he gives some neat illustrations of using interfaces instead of inheritance along with examples of how a "fragile base class" can hurt you.
 
reply
    Bookmark Topic Watch Topic
  • New Topic