File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Difference between abstract class and interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Difference between abstract class and interface" Watch "Difference between abstract class and interface" New topic

Difference between abstract class and interface

Katerina Karen

Joined: Oct 06, 2003
Posts: 6
when should i use an abstract class and when should I use an interface
Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
Here is what i do
If u want to specify partial implementation of ur class make it abstract else make it interface

Prakash Dwivedi (SCJP2, SCWCD, SCBCD)
"Failure is not when you fall down, Its only when you don't get up again"
Pawan Ramchandani
Ranch Hand

Joined: Aug 15, 2003
Posts: 78
Hi Junior
It's a very good question. This is what I understand.
Abstract class is a class which has atleast one abstract method. Here, to get polymorphic behavior, the subclasses have to implement the method.
An interface is a collection of constants and methods. It can contain either constants or methods or both.
Any unrelated class can implement the interface by providing implementations for its abstract methods. So, interface enable you to expediate polymorphism without necessarily defining subclasses.
1. Interface enables you to expediate polymorphism without the need of subclasses. This is not true for abstract class.
This is the major difference between abstract class and an interface.
2. An abstract class can consist other methods too which are not abstract, whereas an interface can contain only abstract methods.
As far as the advantages of Interface goes, you have already mentioned the most important one that we can implement more than one interface but extend only one class.
An interface is also a very handy means of packaging up constants. You can define constants that will be common to all classes and put them together in an interface. Any class that requires it, has to just implement it.
your question:
Why can't abstract classes simply be replaced by interfaces???
From any application point of view, I feel we should use abstract class when we need to restrict the implementation of abstract methods to only those classes which are related.
Use interface when you have methods and constants which can be used by any class in an application. For e.g Date routine method or standard values like pi, or conversion factors like inch to centimeters, etc.
I think i have explained whatever I could think of and I hope it helps you.

Pawan Ramchandani<br />*******************************<br />SCJP 1.4<br />SCWCD <br />*******************************<br />Everything is okay in the end. If it's not okay, then it's not the end.
Michael Ernest
High Plains Drifter

Joined: Oct 25, 2000
Posts: 7292

Junior nka Katerina --
Please take a closer look at our naming policy. We ask that you post using a display that includes a first and last name.
I will close the threads you've created until you've had a chance to make these changes.

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
I agree. Here's the link:
subject: Difference between abstract class and interface
It's not a secret anymore!