File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes When to use Abstract  Class and when to use Interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When to use Abstract  Class and when to use Interface?" Watch "When to use Abstract  Class and when to use Interface?" New topic
Author

When to use Abstract Class and when to use Interface?

Shailesh Vohra
Greenhorn

Joined: Feb 15, 2010
Posts: 9
How do we decide when we shud use Abstract Class and we shud use Interface .???
Can one explain wid example.?
Jehaan Butt
Ranch Hand

Joined: Feb 05, 2009
Posts: 41
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.


SCJP 6
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
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.


SCJP 6
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13867
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

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

Please UseRealWords. Also, read InterfaceVsAbstractClass and InterfaceVsAbstractClassDiscussion.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247


Yes, I agree!! It's been hard reading a lot of the postings. Thank you for the link to the UseRealWords page.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When to use Abstract Class and when to use Interface?
 
Similar Threads
difference between interface and abstract
abstract class and interface
Abstract Class & Interface
Interface and Abstract Classes
Abstract and Interface