Alex Rivera | Logout

Why do most system architects insist on first programming to an interface?

Asked 2008-09-07T17:40:25.710
27

Almost every Java book I read talks about using the interface as a way to share state and behaviour between objects that when first "constructed" did not seem to share a relationship.

However, whenever I see architects design an application, the first thing they do is start programming to an interface. How come? How do you know all the relationships between objects that will occur within that interface? If you already know those relationships, then why not just extend an abstract class?

Edit
Report

2 Answers

18

I think one of the reasons abstract classes have largely been abandoned by developers might be a misunderstanding.

When the Gang of Four wrote:

Program to an interface not an implementation.

there was no such thing as a java or C# interface. They were talking about the object-oriented interface concept, that every class has. Erich Gamma mentions it in this interview.

I think following all the rules and principles mechanically without thinking leads to a difficult to read, navigate, understand and maintain code-base. Remember: The simplest thing that could possibly work.

answered 2008-09-07T18:07:36.613
0

I think the main reason to use interfaces in Java is the limitation to single inheritance. In many cases this lead to unnecessary complication and code duplication. Take a look at Traits in Scala: http://www.scala-lang.org/node/126 Traits are a special kind of abstract classes, but a class can extend many of them.

answered 2010-01-02T10:35:18.293

Your Answer