aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
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
Author

when to use abstract class & when interface

felix thomas
Ranch Hand

Joined: Jun 10, 2001
Posts: 89
hi,

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
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Felix,
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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, 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.


42
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: when to use abstract class & when interface