This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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


Win a copy of Android Security Essentials Live Lessons this week in the Android 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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30138
    
150

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
 
 
subject: when to use abstract class & when interface
 
Similar Threads
Anstract class, Interface real world scenario
abstract class vs interface
Abstract Class & Interface
abstract class and interface
abstract class vs interface ?