Skip to content
August 6, 2010 / Ben Chun

Interfaces before Inheritance?

One of the great things about finding colleagues is that they give you new ideas. Case in point: Eugene Wu hit me with a really interesting one as we drove to work the other morning. In Java, he teaches interfaces before teaching inheritance.

Now I’ve always thought of interfaces as a language feature designed to solve issues related to multiple inheritance by creating the concept of a special sort of class — an interface — containing only what Java calls “abstract” methods, the things that C++ calls “pure virtual” functions. Because of that perspective, I had never even considered teaching interfaces before teaching inheritance. In my mind, the tension between method call resolution in multiple inheritance versus the benefits of polymorphism motivates the interface as a construct.

But look at the advantages to ignoring that: If we don’t need to really know about method call resolution or method overriding yet, the interface can be presented simply as a contract that an implementing class agrees to meet, and a mechanism for creating new reference data types. This lets us introduce the concept and benefits of polymorphism before we even talk about inheritance! Then when it’s finally time to create a subclass, we already have the basic idea of overriding (providing a new method body for a given signature) and it’s pretty nice that it’s optional.

I can see this flow reducing the cognitive load by pulling apart some of the concepts so students can learn them separately. I’m strongly considering doing this in my AP class this year. It will be a bit tricky since the book I use, Head First Java, introduces the topics in the opposite (traditional) order. But maybe it’s time to break out beyond the structure of that book.

3 Comments

Leave a Comment
  1. Hélène Martin / Aug 6 2010 9:34 am

    I like it. Thanks for the good idea.

    Thankfully, I’m free from the despotic grips of a textbook… ; )

  2. John Kurkowski / Aug 6 2010 10:01 am

    Absolutely agree with this technique. Excited to see how it turns out.

    There is another advantage to it. For me, after learning the power of inheritance first, the traditional order made interfaces seem more like a language afterthought. Why did I need interfaces when I had abstract base classes? So I put more time into understanding and using inheritance, and didn’t care to learn interfaces. I didn’t fully understand interfaces until I entered industry, which is too bad, because that’s actually most of what you (are supposed to) use in a C++ or Java shop, not inheritance.

    Inheritance is as grossly overused as they say. To counter this, engineers say, prefer composition to inheritance. Understanding interfaces is essential to composition.

    http://www.artima.com/cppsource/codestandards3.html

Trackbacks

  1. Abstract « And Yet It Moves

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

%d bloggers like this: