File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Interface and abstract classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface and abstract classes" Watch "Interface and abstract classes" New topic

Interface and abstract classes

Ken Sampson
Ranch Hand

Joined: Sep 26, 2007
Posts: 45

I am confused between an Interface and an abstract class. I would cite an example:

I consider various shape objects esp. square, rectangle etc. and consider a common behavior area which I need to determine. Should I consider an Interface or an abstract class and why?

Ken Sampson
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 290

Abstract classes

Abstract classes are used only when there is a �is-a� type of relationship between the classes.
Interfaces can be implemented by classes that are not related to one another.

You cannot extend more than one abstract class.
You can implement more than one interface.

Abstract class can implemented some methods also.
Interfaces can not implement methods.

With abstract classes, you are grabbing away each class�s individuality.
With Interfaces, you are merely extending each class�s functionality.

Ken Sampson
Ranch Hand

Joined: Sep 26, 2007
Posts: 45
Thanks for pointing out the differences but I am still inn confusion as to the choice between an interface or an abstract class in the above scenario. Could you please let me know what would be appropriate in the above scenario.
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

You can also try to read this FAQ.

[My Blog]
All roads lead to JavaRanch
I agree. Here's the link:
subject: Interface and abstract classes
It's not a secret anymore!