This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Java in General and the fly likes when to use abstract class & when interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Java in General
Bookmark "when to use abstract class & when interface" Watch "when to use abstract class & when interface" New topic

when to use abstract class & when interface

felix thomas
Ranch Hand

Joined: Jun 10, 2001
Posts: 89

could any one with real life explain me when we should use abstract class and when we should use interface.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32481

You use an interface when you are defining a contract for the class. For example, "all Display objects will implement print()."

You use an abstract class when you want to provide some base functionality. You also use an abstract class when you want to provide a template and the caller just has to fill in a few methods that get called in the middle.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Michael Herrmann
Ranch Hand

Joined: Dec 06, 2003
Posts: 60
I use abstract classes that don't contain a single non-abstract method as well because some classes will always be only one thing and are never going to be another at the same time. I use abstract classes in this case in order to minimize the sources of bugs and bad design (classes that do too much).
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
once you understand the different concepts ruling abstract classes and interfaces you'll know when to use which...

An interface defines a contract the implementing class promises to abide by.
An abstract class tells you that the extending class IS something.

Have you checked out Aspose?
subject: when to use abstract class & when interface
It's not a secret anymore!