This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between Interfaces and abstract classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between Interfaces and abstract classes" Watch "Difference between Interfaces and abstract classes" New topic
Author

Difference between Interfaces and abstract classes

Shafeeq Sheikh
Ranch Hand

Joined: May 12, 2000
Posts: 68
Hi Guy's.....
This might sound like a very novice question but its been bothering me for some time and I have to ask this....
An interface is a collection of methods which have to be implemented by the class that is implementing this interface....
At the same time an abstract class is a class with a collection of one or many 'empty' methods which have to be implemented by any class that extends this abstract class.....
My question is that what excatly is the difference between an interface and an abstract class..... and in what circumstances is one preferred to be used over the other.???
I know that due to single-inheritance, Java classes can extend only one class and to overcome this limitation, one can use interfaces..... but then what is the purpose of having abstract classes.....???
Why can't abstract classes simply be replaced by interfaces???
Thanks in advance......
Shafeeq Sheikh
Suma Narayan
Ranch Hand

Joined: Apr 03, 2000
Posts: 136
Hi Shafeeq,
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.
Differences
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.




[This message has been edited by Suma Narayan (edited May 13, 2000).]
Shafeeq Sheikh
Ranch Hand

Joined: May 12, 2000
Posts: 68
Got it!!!
Thanks a bunch
 
jQuery in Action, 2nd edition
 
subject: Difference between Interfaces and abstract classes
 
Similar Threads
what is difference between interface and abstract class --- interview question
interface and abstract class
Difference in Abstract classes and Interfaces
which senario we use abstract and interface in java
Difference between abstract class and interface