Why are interfaces preferred to abstract classes?

12,996

Solution 1

That interview question reflects a certain belief of the person asking the question. I believe that the person is wrong, and therefore you can go one of two directions.

  1. Give them the answer they want.
  2. Respectfully disagree.

The answer that they want, well, the other posters have highlighted those incredibly well. Multiple interface inheritance, the inheritance forces the class to make implementation choices, interfaces can be changed easier.

However, if you create a compelling (and correct) argument in your disagreement, then the interviewer might take note. First, highlight the positive things about interfaces, this is a MUST. Secondly, I would say that interfaces are better in many scenarios, but they also lead to code duplication which is a negative thing. If you have a wide array of subclasses which will be doing largely the same implementation, plus extra functionality, then you might want an abstract class. It allows you to have many similar objects with fine grained detail, whereas with only interfaces, you must have many distinct objects with almost duplicate code.

Interfaces have many uses, and there is a compelling reason to believe they are 'better'. However you should always be using the correct tool for the job, and that means that you can't write off abstract classes.

Solution 2

In general, and this is by no means a "rule" that should be blindly followed, the most flexible arrangement is:

interface
   abstract class
       concrete class 1       
       concrete class 2

The interface is there for a couple of reasons:

  • an existing class that already extends something can implement the interface (assuming you have control over the code for the existing class)
  • an existing class can be subclasses and the subclass can implement the interface (assuming the existing class is subclassable)

This means that you can take pre-existing classes (or just classes that MUST extend from something else) and have them work with your code.

The abstract class is there to provide all of the common bits for the concrete classes. The abstract class is extended from when you are writing new classes or modifying classes that you want to extend it (assuming they extend from java.lang.Object).

You should always (unless you have a really good reason not to) declare variables (instance, class, local, and method parameters) as the interface.

Solution 3

You only get one shot at inheritance. If you make an abstract class rather than an interface, someone who inherits your class can't also inherit a different abstract class.

Solution 4

You can implement more than one interface, but you can only inherit from a single class

Solution 5

Abstract Classes

1.Cannot be instantiated independently from their derived classes. Abstract class constructors are called only by their derived classes.

2.Define abstract member signatures that base classes must implement.

3.Are more extensible than interfaces, without breaking any version compatibility. With abstract classes, it is possible to add additional nonabstract members that all derived classes can inherit.

4.Can include data stored in fields.

5.Allow for (virtual) members that have implementation and, therefore, provide a default implementation of a member to the deriving class.

6.Deriving from an abstract class uses up a subclass's one and only base class option.

Interface

1.Cannot be instantiated.

2.Implementation of all members of the interface occurs in the base class. It is not possible to implement only some members within the implementing class.

3.Extending interfaces with additional members breaks the version compatibility.

4.Cannot store any data. Fields can be specified only on the deriving classes. The workaround for this is to define properties, but without implementation.

5.All members are automatically virtual and cannot include any implementation.

6.Although no default implementation can appear, classes implementing interfaces can continue to derive from one another.

Share:
12,996
Techmaddy
Author by

Techmaddy

I believe in action rather than words. Since light travels faster than sound,people appear bright until you hear them speak.

Updated on June 14, 2022

Comments

  • Techmaddy
    Techmaddy about 2 years

    I recently attended an interview and they asked me the question "Why Interfaces are preferred over Abstract classes?"

    I tried giving a few answers like:

    • We can get only one Extends functionality
    • they are 100% Abstract
    • Implementation is not hard-coded

    They asked me take any of the JDBC api that you use. "Why are they Interfaces?".

    Can I get a better answer for this?