Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract vs. Interfaces

 
Nicholas Turner
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
thanks,
nick :
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nicholas Turner
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gotcha a just read the chapter in deitel and deitel and I understand it fully now. thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic