Help coderanch get a
new server
by contributing to the fundraiser
    Bookmark Topic Watch Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Note: This page should be updated to reflect Java 8's interface default methods.


There are three differences between an interface and an abstract class:

  • you can implement multiple interfaces at the same time, but only extend one class,
  • an abstract class is allowed to contain implementation (non-abstract methods, constructors, instance initializers and instance variables) and non-public members, and
  • abstract classes may be a tiny bit faster (or they may not.)


  • Actually the first point is the reason for the existence of interfaces in Java: to provide a form of multiple inheritance. In languages with multiple implementation inheritance, an interface would be equivalent to a fully abstract class (a class with only public abstract members).

    The above differentiation suggests when to use an abstract class and when to use an interface:

  • use an abstract class, if you want to provide common implementation to subclasses,
  • use an abstract class, if you want to declare non-public members,
  • use an abstract class, if you want to be free to add new public methods in the future,
  • use an interface if you're sure the API is stable for the long run
  • use an interface if you want to provide the implementing classes the opportunity to inherit from other sources at the same time.
  • use an interface if you model a system that needs not implementation inheritance but behavioral inheritance (inherit behaviour i.e. methods headers but not implementation/code as can be in abstract classes)


  • In general, prefer interfaces if you don't need to use an abstract class, because they provide more design flexibility.

    11/29/07 Stan James: Added two bullets about the ability to add methods to an abstract class without breaking any clients. If you add a method to an interface, you break all implementations. This puts a different spin on "flexibility" in the paragraph above. This distinction did not occur to me on my own; I just ran across it in Kent Becks Implementation Patterns.

    See InterfaceVsAbstractClassDiscussion


    Other resources:

  • http://mindprod.com/jgloss/interfacevsabstract.html
  • http://www.javaworld.com/javaworld/javaqa/2001-04/03-qa-0420-abstract.html
  • http://www.javaworld.com/javaworld/javaqa/2001-08/03-qa-0831-interface.html

  •  
    Don't get me started about those stupid light bulbs.
      Bookmark Topic Watch Topic
    • New Topic