Differences Between Interfaces and Abstract Classes


In many ways an interface is similar to an abstract class. Both are used to provide a blueprint for class functionality, but there are differences. Because Java does not permit multiple class inheritance, Java class hierarchies are linear. A class can have only one direct superclass that itself can have only one direct superclass and so on. If you want an abstract class to define certain functionality for two unrelated classes, the abstract class would have to be placed in a location common to both class hierarchies. You would end up with a lot of abstract classes placed right below the Object class.

You can avoid this situation by imposing the functionality using interfaces. A class can implement any number of interfaces. The interfaces do not have to be part of a class hierarchy (which should be obvious since they aren't classes). Unrelated classes can implement the same interface.

Another difference between abstract classes and interfaces is that abstract classes can implement some of the methods that they define. Interface methods are always abstract.



Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net