• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When to use Abstract Class and when to use Interface?

 
Shailesh Vohra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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.?
 
Jehaan Butt
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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.
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic