File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Abstract vs. Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Abstract vs. Interfaces" Watch "Abstract vs. Interfaces" New topic

Abstract vs. Interfaces

Nicholas Turner
Ranch Hand

Joined: May 24, 2003
Posts: 126
My question would be:
Why would one use an abstract class verses an Interface
With abstract I guess you only have methods and interfaces you have fields and methods predefined
But why would you choose one over the other and what is the importance of abstract classes?
nick :
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There are pros & cons to both that usually lead you to find the right one for the situation.
An abstract class can have code in it. I wrote an abstract handler for a SAX XML parser. It has some significant code in it to help you do a particular kind of parsing. But some of the most important methods must be overridden to do anything useful, so the class is abstract. Pro: Developers who extend it get the benefit of the methods I wrote. Con: They can only extend one class, so they are prevented from extending some other useful thing at the same time.
An interface cannot have code in it. Con: Developers who implement it must write all the required methods. Pro: They can extend some other useful class at the same time.
Some parts of the JDK mix the two. They specify an interface, and also provide a usable implementation or an abstract implementation that you can extend if you like.

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
Nicholas Turner
Ranch Hand

Joined: May 24, 2003
Posts: 126
I gotcha a just read the chapter in deitel and deitel and I understand it fully now. thanks
I agree. Here's the link:
subject: Abstract vs. Interfaces
It's not a secret anymore!