posted 19 years ago
Interfaces and abstract classes are both used when we want to let someone else know the general type of thing they are working with, but not the exact class. So if I tell you I'm going to pass a List to your method, you know the object will have all the methods and such defined by the List interface but you don't know exactly what class I might send. That gives me the freedom to use an ArrayList, TreeList or whatever best fits my needs without affecting you. You'll see interfaces used this way a lot in the JDK. You can get an Iterator from a List. You are assured that the Iterator implements the Iterator interface, but you don't know exactly what class the Iterator will be.
Abstract classes are similar in that you can't get a real instance of an abstract class. You will always get an instance of something that extends it, but you don't have to know exactly what it is. An extra benefit of abstracts comes when it's your job to extend one. You have to provide concrete implementations of all the abstract methods. But the abstract class may provide some built-in or default behavior to help you out.
For example, I'm working on a server that has many "work classes". The abstract work class has a method "currentUser()" that returns the userid of the person who submitted the request to the server. It's just a little helper method that saves me from writing four or five lines of code every time I need a userid.
Hope that helped!
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi