Yakov Fain's Blog

My notes about everything in IT

What’s the difference between abstract classes and interfaces?

with 3 comments


Probably the most popular question during Java technical interviews is “What’s the difference between abstract classes and interfaces”.  In my own three year old book I’ve offered the following answer:

“An abstract class may contain code in method bodies, which is not allowed in an interface.With abstract classes you have to inherit your class from the abstract one because Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.”

But starting from Java 8 this answer is wrong. This is how I’d answered it now:

1. The short answer: “Abstract classes can implement state in member variables, but interfaces can’t.  You can extend from one abstract class, but implement multiple interfaces.”

2. The long one: “Both abstract classes  and interfaces allow you to implement common static methods while leaving the implementation of some methods to other classes. Abstract classes can also have instance-specific methods and they enforce vertical inheritance hierarchy.  Interfaces can’t have instance methods, but they don’t enforce any class hierarchy, so your class can extend any other class while implementing multiple interfaces.

So abstract classes and interfaces have subtle differences now, which are good to recognize anyway. You can find more details on this subject in the draft of Chapter 5 of my upcoming Java for Kids book.

About these ads

Written by Yakov Fain

August 18, 2014 at 4:56 pm

Posted in java

3 Responses

Subscribe to comments with RSS.

  1. When I answered this question during my last (and successful) job interview I gave the ‘old’ answer first, and after that I mentioned Java8 improvement with default method – You wanna show to the interviewer that you’re technically up-to-date, not that you’re ahead of him/her!

    Uros

    August 20, 2014 at 9:03 am

  2. Java training is a general purpose programming language, it highlights all of the key differences. If an abstract class implements an interface, it does not hold to actually define the interface’s methods. However, a particular class must implement all interface methods that are not implemented by its parent class. It is a component of the Java technology concept of interfaces.

    arti

    August 28, 2014 at 5:38 am

  3. My try: an interface defines some contract and may not have state (fields); abstract classes may both define some contract and provide its partial implementation; abstract classes can hold some state (fields) to support this partial implementation.

    This definition should work across all Java versions :)

    Actually, the absense of static methods in pre-8 Java was a strange ommision, while nothing in jvm, bytecode instructions or class file format prevents them.

    Valery

    August 28, 2014 at 10:22 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 151 other followers

%d bloggers like this: