By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,797 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,797 IT Pros & Developers. It's quick & easy.

The difference between an abstract class and an Interface?

P: n/a
What is the difference between an abstract class and an Interface.

As a novice, they both seem to serve the same purpose?

Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
G. Smith Q news wrote:
What is the difference between an abstract class and an Interface.

As a novice, they both seem to serve the same purpose?


An abstract class can provide (partial) implementation. A derived class
may only have one parent class, so a class cannot inherit from multiple
abstract classes (that do not already inherit from each other).

An interface provides a contract. It does not provide any
implementation. A class may implement many interfaces.

If you are unsure if you need one or the other, use both. Create an
interface and an abstract class that implements the interface (see
java.util.List and java.util.AbstractList for a good example). Access
instances via the interface unless that specific implementation is
required. For example:

List myList = new ArrayList();
myList.add("blah");

In general, prefer to use interfaces over abstract classes. That is,
the default position should be that you should use an interface and you
should have to convince yourself that in such and such a case, you
really need an abstract class.

HTH,
Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.