Alex Rivera | Logout

Why are interfaces preferred to abstract classes?

Asked 2009-03-12T17:15:23.663
39

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?

Edit
Report

4 Answers

23

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.

answered 2009-03-12T17:17:49.853
10

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

answered 2009-03-12T17:18:05.093
3

"Why Interfaces are preferred over Abstract classes?"

The other posts have done a great job of looking at the differences between interfaces and abstract classes, so I won't duplicate those thoughts.

But looking at the interview question, the better question is really "When should interfaces be preferred over abstract classes?" (and vice versa).

As with most programming constructs, they're available for a reason and absolute statements like the one in the interview question tend to miss that. It sort of reminds me of all the statement you used to read regarding the goto statement in C. "You should never use goto - it reveals poor coding skills." However, goto always had its appropriate uses.

answered 2009-03-12T18:01:30.120
3

Respectfully disagree with most of the above posters (sorry! mod me down if you want :-) )


First, the "only one super class" answer is lame. Anyone who gave me that answer in an interview would be quickly countered with "C++ existed before Java and C++ had multiple super classes. Why do you think James Gosling only allowed one superclass for Java?"

Understand the philosophy behind your answer otherwise you are toast (at least if I interview you.)


Second, interfaces have multiple advantages over abstract classes, especially when designing interfaces. The biggest one is not having a particular class structure imposed on the caller of a method. There is nothing worse than trying to use a method call that demands a particular class structure. It is painful and awkward. Using an interface anything can be passed to the method with a minimum of expectations.

Example:

public void foo(Hashtable bar);

vs.

public void foo(Map bar);

For the former, the caller will always be taking their existing data structure and slamming it into a new Hashtable.


Third, interfaces allow public methods in the concrete class implementers to be "private". If the method is not declared in the interface then the method cannot be used (or misused) by classes that have no business using the method. Which brings me to point 4....


Fourth, Interfaces represent a minimal contract between the implementing class and the caller. This minimal contract specifies exactly how the concrete implementer expects to be used and no more. The calling class is not allowed to use any other method not specified by the "contract" of the interface. The interface name in use also flavors the developer's expectation of how they should be using the object. If a developer is passed a

public interface FragmentVisitor {
    public void visit(Node node);
answered 2009-03-15T07:34:00.457

Your Answer