• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic