Abstract class VS Interface

When you are going to change your job you must be ready for the interview. Yet not only on this occasion you should know answers to theoretical questions. A real professional keeps fundamental knowledge in mind even not being aware of it. One of such things in Java is the difference between abstract class and interface.

Let’s look into their essential distinctions. After all, everyone needs to find out the difference and the purpose of interface and abstract class.

Method implementation

Abstract class can contain method implementation unlike interface, that has, just declaration and no method realization.

Inheritance

Any class can inherit only one abstract class. As with interfaces,┬áthere is another situation – class can implement more than one interface.

Access modifiers

Non-abstract fields of abstract class can have any access modifiers (private, public, protected), they can even be static or final. But abstract fields can be just public or protected. Otherwise how can they be implemented or overridden in the following realizations?

Interfaces can have only abstract and public fields, by the way this state is set as default for the methods in the interfaces. This is the main reason why you need to implement all methods which are declared in the interface. Interface can contain variables only as public final static. This automatically make them constants.

Constructor

Abstract class allows you to declare constructors. You can’t do the same while programming interfaces.

Basing on this comparison, make your own decisions as to when and what will be better in your programming practice.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
  • http://about.me/eazyigz Igor Ganapolsky

    A good high-level overview. Every professional Java programmer should refresh his memory with these once in a while.

    • http://fruzenshtein.com/ Alex Zvolinskiy

      Absolutely right!