• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

When to use Abstract Class and when to use Interface?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do we decide when we shud use Abstract Class and we shud use Interface .???
Can one explain wid example.?
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should use an abstract class when you:

- need to have some concrete method implementations within the superclass itself, and a few abstract methods which would be implemented by the concrete subclasses.
- are absolutely sure that the abstract class' subclasses will not need to extend any other class. Remember, Java allows you to extend only one class.

When you use an interface, you:

- cannot provide an implementation for any method you create. In other words, all the methods that are defined are implicitly abstract.
- can implement the interface in another class and also, extend another class and implement many more interfaces as needed.

Bottomline - Interfaces allow you to implement as many interfaces as you want, and you are also allowed to extend one class. But the trade off is that you must implement ALL of the interface's methods in the first concrete (non-abstract) implementing class.

The K&B book has numerous examples of this.
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an excellent explanation from Anurag.

I just want to add that in real world situations, I found that the Interfaces are usually created by the lead developer or designer of the software development. Then the rest of the development team are tasked to created whatever abstract classes and concrete classes by using those Interfaces.
In other words, based on what Anurag said, the senior developers decide on and design the Interfaces to mandate all the required functionalities (i.e., the methods) without having to write all the necessary code. Then that is followed by further development of abstract and concrete classes. Therefore in a collaborative work environment, it's the process that decides most of the choices between Interface and abstract class.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

This questions sounds like it is not directly related to the SCJP exam. I will move it to a more appropriate forum for you.
 
Sheriff
Posts: 22802
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shailesh Vohra wrote:...shud...shud...wid


Please UseRealWords. Also, read InterfaceVsAbstractClass and InterfaceVsAbstractClassDiscussion.
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:
Please UseRealWords. Also, read InterfaceVsAbstractClass and InterfaceVsAbstractClassDiscussion.



Yes, I agree!! It's been hard reading a lot of the postings. Thank you for the link to the UseRealWords page.
 
This tiny ad is suggesting that maybe she should go play in traffic.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic