Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Interface + abstract class + impl why ?

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody
I have seen many people use this pattern and cannot figure what does it really buy you ?
You have an interface which and abstract class implements an dfinally you have ur implementation that extends the abstract class
Ex:
public interface Command {
.....
}

abstract class TransactionalCommand implements Command {
....
}

class UpdateCommand extends TransactionalCommand {
....
}

What does this buy me ? why do i need to abstract the implmentation ?
why not use
public interface Command {
.....
}

class UpdateCommand implements Command {
....
}


I would greatly appreciate your feedback than you
-Reeve
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question!

In this context, the abstract class can be a way to provide a "basic" (or partial) implementation of the interface, making it easier for concrete classes to use the interface.

For example, suppose the interface declares 30 methods. Perhaps 25 of these can be implemented in a basic way (in an abstract class), leaving only 5 that a programmer must write implementation for in a concrete class. This is a great convenience. And of course, any basic implementation in the abstract class could still be overridden if needed.

For a good example of this in the API, see the class description for java.util.AbstractList.
[ December 14, 2005: Message edited by: marc weber ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the theory is that if there's some code that many or most (but probably not all) of your Command implementations will need, putting this code into an AbstractCommand is a convenient way to allow the code to be re-used by concrete Command implementations. However often this technique can increase the chance of errors in your code, as it can be easy for people to make mistakes when extending classes. I recommend Effective Java (Items 14-16 in particular) for more discussion of this point, and alternate strategies.
 
Jason Rodrigues
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys for your quick response , you guys are really wonderful . I appreicate you insight and why it might be applied . It makes more sense now and thank you pointing me in the right direction . I guess its time ot read some more
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also don't mistake that there's more or less two separate concepts being combined here. The first being that an interface defines the contractual obligations rather than a concrete type, which I agree with. The second being that it's a good idea to provide an abstract implementation of an interface, which I disagree with. The advantages of defining the contract through an interface should be obvious: it allows clients to define type by an interface and allows for multiple implementations that are interchangeable.

The second issue boils down to whether or not you think concrete inheritance is appropriate. Some will go so far as to call concrete inheritance a language defect and believe it should never be used, others think it's great to use all the time. I think most programmers fall somewhere in between, believing it is appropriate sometimes though not necessarily agreeing on when. I personally think concrete inheritance is rarely appropriate I agree with Joshua Bloch that you should "prefer composition over [concrete] inheritance." Usually any benefits to be had from creating an abstract class for an interface could be better served using composition and a class whose responsibility is to provide the common functionality desired. I'm actually tempted to say that's true always but I'm not experienced enough to be so set in my ways, so I'll leave the possibility open.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's probably a hint in here that if an interface is so big that you need an abstract default implementation so you only have to override a few select methods, the interface was too big to start with. With other people's interfaces you may be stuck with this. With your own, see if you can't make a more focused design by dividing up the interface.

This practice gives a lot of us theoretical pause, but I just extended the abstract FilterInputStream the other day and was glad I didn't have to implement every method.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stan James:
There's probably a hint in here that if an interface is so big that you need an abstract default implementation so you only have to override a few select methods, the interface was too big to start with.



Often there's also the aesthetic consideration, though. The classic example of this sort of "interface adapter" are the java.awt.event.XXXAdapter classes; the interfaces have fewer than a half-dozen methods, but if you only need to implement one, it's just plain ugly to have to see

public void doDiddlyOne(DiddlyEvent unused) {}
public void doDiddlyTwo(DiddlyEvent unused) {}
public void doDiddlyThree(DiddlyEvent unused) {}

In your code. So the event adapters do some good, even though they do nothing at all!
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stan James:
I just extended the abstract FilterInputStream the other day and was glad I didn't have to implement every method.



Now image someone writing a database driver: interface java.sql.ResultSet has (... ResultSet.class.getMethods().length ...) 139 methods! Let's not
even mention its subinterfaces!

Implementation inheritance come to Papa!
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeff Albrechtsen:


Now image someone writing a database driver: interface java.sql.ResultSet has (... ResultSet.class.getMethods().length ...) 139 methods! Let's not
even mention its subinterfaces!

Implementation inheritance come to Papa!



Perfect Symbiotism of Contractual Operations come to Papa!
Interface inheritance, as we currently define it, assumes that time does not move. We are such silly creatures.
[ December 15, 2005: Message edited by: Tony Morris ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True story: I was visiting the town where I had lived for a spell (ah, the Moosewood Restaurant), and I saw the bumper sticker "My child is an honor role student" on a parked car. Just around the corner was a pickup truck with another bumper sticker "My kid just beat up your honor role student"

So, before our kids commence to fighting, could you locate for me a dictionary that defines Symbiotism?
 
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
I learned all about Symbiotism on Star Gate on the Skiffy channel.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeff Albrechtsen:
"My child is an honor role student" on a parked car. Just around the corner was a pickup truck with another bumper sticker "My kid just beat up your honor role student"



Am I spoiling your irony, or enhancing it, by pointing out that it's "honor roll"?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:

Am I spoiling your irony, or enhancing it, by pointing out that it's "honor roll"?



A little of both.
 
reply
    Bookmark Topic Watch Topic
  • New Topic