jQuery in Action, 2nd edition
The moose likes Java in General and the fly likes abstract class vs interface Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "abstract class vs interface" Watch "abstract class vs interface" New topic
Author

abstract class vs interface

Raghuveer Rawat
Ranch Hand

Joined: Apr 03, 2003
Posts: 102
Anyone who have idea when we should use abstract class and when interface.


Raghuveer Rawat<br />SCJP2
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8265

The Java Tutorial mentions both. It should be easy to figure out the appropriate use of each.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
See this thread for a "world of nature" example of interface vs. abstract class.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
Here are two general guidelines that might help:
First: An interface cannot contain any implementation whatsoever. (Thus, all methods are abstract.) An abstract class can contain as much implementation as you desire. (For example, an asbstract class can contain one or more fully implemented methods.) Therefore, if you want to supply a partial implementation, use an abstract class. Otherwise, use an interface.
Second: A class can implement multiple interfaces. It can only inherit one abstract class. Thus, if you want a class to be able to implement two or more "interfaces", they must be interfaces, not abstract classes.


For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
Correct me if I'm wrong. To me, interface in java is about as close as java gets with multiple inheritance, remember, java doesn't allow multiple inheritance. However, interface allows you to do that in a sense.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract class vs interface
 
Similar Threads
When to use Abstract class and when to use Interface ?/
use of abstact class and interface
abstract class vs interface ?
Abstract and Interface
Abstract Class & Interface