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.