Monday, September 18, 2006

Abstract Class or Interface

Developers are often confused with the choice of abstract class or interface. In design patterns, for example, factory method and abstract factory, abstract classes are used sometimes and interfaces other times.

What is exactly the difference between abstract class and interface? One simple idea is the relationship. Abstract class hints a "is-a" relationship and interface a "is" . For example, a Dog is an animal, and a Dog is priceable, which means it can have a price and participate in a market.
Most introductory Java texts take an implementation-centric stab at how to use interfaces and abstract classes. However, few provide a clear design distinction for choosing between these two similar object-oriented constructs. This article investigates such a distinction through a discussion of a simple, yet common, example. The resulting design uses both interfaces and abstract classes to maximize flexibility and extendibility.
Please read the Maximize flexibility with interfaces and abstract classes for deep understanding of their differences.

No comments: