File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes abstract Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "abstract" Watch "abstract" New topic


linta sebastian

Joined: Dec 07, 2006
Posts: 11
when to use abstract or interface in an project
Alex Sotin

Joined: Dec 19, 2006
Posts: 21
When To Use Interfaces
An interface allows somebody to start from scratch to implement your interface or implement your interface in some other code whose original or primary purpose was quite different from your interface. To them, your interface is only incidental, something that have to add on to the their code to be able to use your package.

When To Use Abstract classes
An abstract class, in contrast, provides more structure. It usually defines some default implementations and provides some tools useful for a full implementation. The catch is, code using it must use your class as the base. That may be highly inconvenient if the other programmers wanting to use your package have already developed their own class hierarchy independently. In Java, a class can inherit from only one base class.

When to Use Both
You can offer the best of both worlds, an interface and an abstract class. Implementors can ignore your abstract class if they choose. The only drawback of doing that is calling methods via their interface name is slightly slower than calling them via their abstract class name.

<a href="" target="_blank" rel="nofollow">Java Planet</a> <br />-- <a href="" target="_blank" rel="nofollow">Java Index</a>
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
calling methods via their interface name is slightly slower than calling them via their abstract class name.

I would like to see some evidence for that assertion.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63849

linta sebastian, please take the time to post your questions in the apporpriate forum. This forum is for questions on JSP.

I have moved this post to a more appropriate location.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: abstract
It's not a secret anymore!