Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which one to choose abstract class vs interface while designing java code

 
Praveen Chaudhary
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
In which cases abstract classes are best choice then interface and vice versa.Real time example explanation is highly appreciated.
Thanks
Praveen
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstraction is the most elegant part of the Java language, and an interface is the most abstract component available. So, if the question is 'which one is more flexible and pluggable', then definitely the Interface is the right answer.

Look at the Servlet and JSP API. It seems like every class we take for granted, is an interface.

But obviously, not everything is an interface. Always use the right tool for the job.

-Cameron McKenzie
 
Chaminda Amarasinghe
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Praveen,

I think you are in wrong forum, These type of questions will be discussed freequesntly in SCJP forum and General Java forum

For your question, abstract classes enable reusing and interfaces enable flexibility and playability. See J2SE code for real world examples.


Thanks
 
stanislav bashkirtsev
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interface - has no any implementaion and it's possible to implement several interfaces.
Abstract class - useful when you want to implement some methods.
If you want to write only signature - use interface. If you want to implement some code - Abstract class. You can combine these things: write an interface and implement some most important methods in abstract class(the most usual situation)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic