<p>My laundry list:</p>

<ol>
<li>We write sample programs using components from a library (say JavaFx, or SFML)</li>
<li>We don't care (much) about the internals of objects, as long as they work, but about their programming interface = what we can do with them. Abstraction.</li>
<li>Anyway, we're going to build new objects. Some explanations about <code>class</code>es/methods/fields. Encapsulation. </li>
<li>Some details are better kept <code>private</code>. Enforcement of abstraction.</li>
<li>Objects are meant to be used through their API, so we first have to design the API. How we want innocent programmers to use them. Objects as service providers.</li>
<li>Enter the notion of (java) <code>interface</code> (sorry, C++ guys) as "contract", and classes realizing them.</li>
<li>Some examples of classes with the same interface. Polymorphism.</li>
<li>Some classes have lots in common, introduce abstract class to factorize code, and inheritance.</li>
<li>designing abstract classes in order to be a sound basis for concrete classes (some patterns).</li>
<li>sometimes, inheritance from a concrete class.</li>
</ol>

<p>Students are exposed to polymorphism at a early stage: the example is a "guess a random number" game. The game is parameterized (?) by a <code>Generator</code>. During debugging phases a <code>FakeGenerator</code> is used, which always returns the same value.</p>

<pre><code>public interface Generator {
   int getNumber();
}

public class FakeGenerator  implements Generator {
   final int fixedValue;
   public FakeGenerator(int value) {
       fixedValue = value;
   }
   @Override
   public int getNumber() {
       return fixedValue;
   }
}
class Game {
    Generator generator;
    Game(Generator generator) {
         this.generator = generator;
    }
...
}
Game game = new Game( 
                   // new UniformGenerator(1,10) // 
                      new FakeGenerator(33)  // for debugging
                    );
</code></pre>

CC BY-SA 3.0
2018-02-08T10:34:29.250Z
2018-02-08T13:29:37.053Z
2018-02-08T13:29:37.053Z
Michel Billaud
<p>Interfaces should be taught right after, or during the section on APIs, because they are simply a promise to implement a certain Application Programming Interface. It's why they are called interfaces in the first place. They are also more general than inheritance, which makes them a much better example as a polymorphic data type. With this in mind, I propose that after APIs, the following concepts are taught in order:</p>

<ol>
<li><strong>Interfaces:</strong> This is language support for what was taught during APIs, and introduces the 'duck principle'. That is, if it walks like a duck, quacks like a duck, and behaves like a duck, its a duck. This is a good argument and explanation for the need for polymorphism.</li>
<li><strong>Polymorphism:</strong> What good is an interface if we can't swap out one implementation for another? Take for example, Java's <code>List</code> interface, and two of its realizations: <code>LinkedList</code> and <code>ArrayList</code>; without polymorphism, we can't write generic code that targets both of these types.</li>
<li><strong>Abstract Classes:</strong> Sometimes, when writing two different realizations of the same interface, we see that code can be shared between different types, while fundamental portions of the implementation remain separated. Take, for example, designing a <code>HumanPlayer</code>and <code>ComputerPlayer</code> for a card game. Functionality relating to scoring and storing cards will be shared, but the act of choosing which card to play is fundamentally different, and cannot be shared. The interface, however, needs to be consistent.</li>
<li><strong>Inheritance:</strong> This is primarily for code reuse. Additional functionality or desired traits are added to a type that already is an object in its own right. Taking advantage of a somewhat nebulous implementation (such as extending Java's <code>Exception</code> class) is another use case. You are customizing more than you are providing functionality. Unlike the other strategies listed, you don't have to use inheritance to implement an API. You could, for example, convert a regular binary tree into a balancing binary tree.</li>
</ol>

<p>Teaching these concepts in this order should help to showcase the use cases for each of these features. Interfaces were brushed over when I was learning programming, and it took me a while to understand what their use case was; I would try to use inheritance anywhere I needed polymorphism, which doesn't make sense in the grand majority of cases. This teaching order should get over that misconception.</p>

CC BY-SA 3.0
2018-01-24T18:53:46.337Z
2018-01-24T18:59:21.903Z
2018-01-24T18:59:21.903Z
Soupy
<p>Ideally, interfaces come first. Before Classes. Interfaces define <em>concepts</em>. Classes implement those concepts. Don't think of them as an add-on to OO programming. Think of them as the essence. </p>

<p>In fact, if you present a pre built class to students, my guess is that you already, probably informally, give the interface first. Here is a class with public methods "accept" and "spitOut", or something like that. You probably talk about the public methods first and the necessary parameters and return values. More importantly, you probably talk about the <em>intent</em> of the class and of the individual methods. Most likely, and I think most common, only then do you discuss the implementation of the methods along with fields, etc. </p>

<p>If you start your discussion with the fields, on the other hand, you are (IMO) making a big mistake and that students will start out by thinking at too low a level and will write their code too often in terms of getters and setters. In other words the encapsulation will be inconsequential and possibly leaky. Then, in a program with a few tens of classes and a few hundreds of objects, but, perhaps thousands of fields, they will be thinking about the fields and how they mutate, rather than the concepts represented by the objects in the system. Their thinking will be at a lower level than is efficient (or even possible, in some complex programs). </p>

<p>But if you introduce the concepts first, and get them to (a) think in terms of those concepts and (b) implement those concepts faithfully they will be naturally led to think at a higher level of abstraction, which is really what programming languages are <em>for</em> in the first place. </p>

<p>Some rules that I'd adhere to with rare exceptions and no exceptions at the start. </p>

<blockquote>
  <ol>
  <li>When you implement an interface don't give the class any additional public methods. Thoroughly document the intention of the interface and its methods.</li>
  <li>When you build a subclass of an existing concrete class, don't give the subclass any public methods that don't have signatures in the superclass. (Abstract classes can be a bit different, but it is dangerous and adding public methods should be delayed.)</li>
  <li>If you need new things in a subclass, extend the interface instead, as it represents a new concept, and implement that, rather than just extending the superclass, though you can also extend as an implementation strategy. </li>
  <li>Prefer composition of objects over inheritance. An object is composed of other simpler objects (representing their own concepts). With composition you will probably be able to 
  
  <blockquote>
    <p>a. Write simple classes (a few public methods)<br>
    b. having simple methods. I get antsy when a method is more than about 4 lines or has any nested internal structure. </p>
  </blockquote></li>
  <li>Use simple design patterns to make it fit together. These are just another sort of concept and are themselves normally defined with interfaces.  </li>
  <li>Avoid getters and setters (in interfaces and classes). They lead the student to think of everything in terms of mutations of implementation, rather than at a high level. I use <em>accessor</em> for a method that returns information (possibly a field's value, but not always or often), and <em>mutator</em> for a method that somehow changes the internal state of an object. A mutator might in fact have nothing to do with fields and might pass on a message to one of the implementing objects or set up a chain of mutations. Thinking in terms of setters is too narrow, but also too low level. 
  
  <hr></li>
  </ol>
</blockquote>

<p>Some modern OO languages don't have formal interfaces, but you can normally simulate them to separate out the ideas from the implementations. </p>

<hr>

<p>Note that the Standard Template Library (which I haven't used in quite a while) was, itself defined in terms of <em>concepts</em> and the term was (maybe still is) used in the literature. This was just a placeholder for interfaces that C++ didn't support. </p>

<p>The idea here is to build a software system in terms of readily understood concepts. The resulting system has its complexity in the <em>interactions</em> of simply understood things, rather than in the things themselves. </p>

<hr>

<p>Note that I often write an interface when I intend only one implementing class. Capture the concept first. </p>

<p>Again, note that I often write an abstract class that has protocol (abstract methods) for every method in any intended subclass. This lets me capture a common implementation, but keeps my concept clear as I subclass. I never need to ask <em>instanceof</em> and I never need to cast an object reference. Everything naturally fits together freeing my mind to think of other (higher level) things. </p>

CC BY-SA 3.0
2017-12-23T12:37:20.247Z
I would not expect a student to be able to implement an interface, before they have done classes, and inheritance. But they should be able to inherit an interface that you have made, and implement it. It would also be better to have a full interface (that is one with contracts). This way the student knows what to implement.
ctrl-alt-delor
CC BY-SA 3.0
2017-12-24T15:41:07.763Z
I like that you think in abstract first before coding, but I am surprised that you did not mention use of a different [paradigm](https://en.wikipedia.org/wiki/Programming_paradigm) when programming, e.g. functional or logic. I personally prefer logic, e.g. Prolog or Mercury, or other more abstract means of computation. The problem is that to much of the world is done with the old technology and sometimes using that for their exiting libraries outweighs the value in using another paradigm, e.g. [NumPy](http://www.numpy.org/)
Guy Coder
CC BY-SA 3.0
2018-02-02T15:06:00.533Z
While interfaces are generally developed before the implementing class; that doesn't mean you have to teach in in the same order. Interfaces are built with a consideration to the future (the implementation), which means the student needs to know what implementation entails. Interfaces are the solution to a problem, and therefore you must first showcase the problem to justify using the interface as a solution.
Flater
CC BY-SA 3.0
2018-02-02T15:29:08.257Z
True enough, but interfaces are also good in the thinking stage of problem solving when you are deciding what it is you need. They are like a rough sketch of the capabilities needed.
Buffy
CC BY-SA 3.0
2018-02-08T16:41:38.093Z
I agree with @Buffy here, but I would add that the **idea** of 'Interfaces' be _**introduced**_ first as opposed to being necessarily _**taught**_ in  any order. It is very important for OO students to focus on the _behaviors_ of objects.  You are not required to actually use the terms when you teach the topics. ;)
Mr Bradley
CC BY-SA 3.0
2017-12-22T18:39:18.310Z
2017-12-22T19:20:22.923Z
2017-12-22T19:20:22.923Z
Buffy
<p>I'd say that it can fit very well between the Inheritance and the Polymorphism.</p>

<p>Interfaces are a way of promising that any implementing class supports the interface's functionality. for example, given an interface <code>Movable</code>:</p>

<pre><code>public interface Movable {

    public void move();

}
</code></pre>

<p>one knows that anything that for any object instance of a class which <code>implements Movable</code>, we can call <code>.move()</code>.</p>

<p>This means that, unlike <code>extend</code>ing a class, <code>implements</code> means (and <strong>directly implies</strong>) that instances of the class have some functionality. </p>

<p>Teaching it in this order gives the view of an interface being nothing more than a promise for some functionality, and how such a thing is integrated into OOP. </p>

<p>This also avoids some of the confusion I've witnessed in cases where abstract classes are taught before interfaces. They are not the same, but understanding why it's ok for something not to have a method body (i.e. abstract methods in such a class) is clearer to students if they know interfaces.</p>

<p>Additionally, if one teaches interfaces as a <a href="https://cseducators.stackexchange.com/questions/544/explaining-how-an-interface-is-a-contract" title="A question I asked and got really good answers">contract of functionality</a>, then it doesn't appear as an add-on. It's a fully-fledged OOP concept with incredible usefulness. </p>

<p>Whenever a person needs to describe <strong>just the functionality</strong> required by an unknown set of objects (and maybe of different classes), an interface is their best friend. </p>

CC BY-SA 3.0
2018-02-08T08:27:02.200Z
2018-02-08T08:27:02.200Z
ItamarG3
<p>I've been teaching Objects for years now as part of AP Computer Science A (which means that I am teaching OOP in Java), and I have experimented with putting Interfaces in at different locations.  In general, I currently teach in the following order:</p>

<ul>
<li><strong>Class Structure</strong>: the coupling of data with relevant methods.</li>
<li><strong>Information Hiding</strong>: making each class responsible to guarantee the integrity of its data.</li>
<li><strong>Designing the API</strong>: Given what we've learned, how should we design the public face of our classes?  Included in this section is the concept of overloading.</li>
<li><strong>Inheritance</strong>: Creating relationships among classes, inheriting and overriding.</li>
<li><strong>Polymorphism</strong>: Being able to utilize children in place of their parents, and how that works.</li>
</ul>

<p>Interfaces seem most related to Polymorphism to me, since we can (for instance) make an <code>ArrayList&lt;SomeInterface&gt;</code> but wherever I put it in, it always feels like some add-on instead of like the core and vital concept that it really is.</p>

<p>Where (and how) do you integrate Interfaces into your OOP exposition?</p>

CC BY-SA 3.0
2017-12-24T15:25:45.887Z
When I think `Interface` I always think of it as the primary way to avoid the [multiple inheritance](https://en.wikipedia.org/wiki/Multiple_inheritance) problem. See also [Multiple Inheritance in Java](https://www.journaldev.com/1775/multiple-inheritance-in-java)
Guy Coder
<curriculum-design><object-oriented-paradigm><interface><lesson-order>
Where, sequentially, do Interfaces sit in an introduction to Objects?
# Where, sequentially, do Interfaces sit in an introduction to Objects?

(Asked by: username_0 on 2017-12-22)

I've been teaching Objects for years now as part of AP Computer Science A (which means that I am teaching OOP in Java), and I have experimented with putting Interfaces in at different locations. In general, I currently teach in the following order:

* **Class Structure**: the coupling of data with relevant methods.
* **Information Hiding**: making each class responsible to guarantee the integrity of its data.
* **Designing the API**: Given what we've learned, how should we design the public face of our classes? Included in this section is the concept of overloading.
* **Inheritance**: Creating relationships among classes, inheriting and overriding.
* **Polymorphism**: Being able to utilize children in place of their parents, and how that works.

Interfaces seem most related to Polymorphism to me, since we can (for instance) make an `ArrayList` but wherever I put it in, it always feels like some add-on instead of like the core and vital concept that it really is.

Where (and how) do you integrate Interfaces into your OOP exposition?

**Tags:** curriculum-design, object-oriented-paradigm, interface, lesson-order

> **Comments:**
> **username_1**: When I think `Interface` I always think of it as the primary way to avoid the [multiple inheritance](https://en.wikipedia.org/wiki/Multiple_inheritance) problem. See also [Multiple Inheritance in Java](https://www.journaldev.com/1775/multiple-inheritance-in-java)  

---

## 4 answers

### Answer by username_4 on 2017-12-22. Score: 8

Ideally, interfaces come first. Before Classes. Interfaces define *concepts*. Classes implement those concepts. Don't think of them as an add-on to OO programming. Think of them as the essence.

In fact, if you present a pre built class to students, my guess is that you already, probably informally, give the interface first. Here is a class with public methods "accept" and "spitOut", or something like that. You probably talk about the public methods first and the necessary parameters and return values. More importantly, you probably talk about the *intent* of the class and of the individual methods. Most likely, and I think most common, only then do you discuss the implementation of the methods along with fields, etc.

If you start your discussion with the fields, on the other hand, you are (IMO) making a big mistake and that students will start out by thinking at too low a level and will write their code too often in terms of getters and setters. In other words the encapsulation will be inconsequential and possibly leaky. Then, in a program with a few tens of classes and a few hundreds of objects, but, perhaps thousands of fields, they will be thinking about the fields and how they mutate, rather than the concepts represented by the objects in the system. Their thinking will be at a lower level than is efficient (or even possible, in some complex programs).

But if you introduce the concepts first, and get them to (a) think in terms of those concepts and (b) implement those concepts faithfully they will be naturally led to think at a higher level of abstraction, which is really what programming languages are *for* in the first place.

Some rules that I'd adhere to with rare exceptions and no exceptions at the start.

> 1. When you implement an interface don't give the class any additional public methods. Thoroughly document the intention of the interface and its methods.
> 2. When you build a subclass of an existing concrete class, don't give the subclass any public methods that don't have signatures in the superclass. (Abstract classes can be a bit different, but it is dangerous and adding public methods should be delayed.)
> 3. If you need new things in a subclass, extend the interface instead, as it represents a new concept, and implement that, rather than just extending the superclass, though you can also extend as an implementation strategy.
> 4. Prefer composition of objects over inheritance. An object is composed of other simpler objects (representing their own concepts). With composition you will probably be able to
>    > a. Write simple classes (a few public methods)  
>    > b. having simple methods. I get antsy when a method is more than about 4 lines or has any nested internal structure.
> 5. Use simple design patterns to make it fit together. These are just another sort of concept and are themselves normally defined with interfaces.
> 6. Avoid getters and setters (in interfaces and classes). They lead the student to think of everything in terms of mutations of implementation, rather than at a high level. I use *accessor* for a method that returns information (possibly a field's value, but not always or often), and *mutator* for a method that somehow changes the internal state of an object. A mutator might in fact have nothing to do with fields and might pass on a message to one of the implementing objects or set up a chain of mutations. Thinking in terms of setters is too narrow, but also too low level.
>
>    ---

Some modern OO languages don't have formal interfaces, but you can normally simulate them to separate out the ideas from the implementations.

---

Note that the Standard Template Library (which I haven't used in quite a while) was, itself defined in terms of *concepts* and the term was (maybe still is) used in the literature. This was just a placeholder for interfaces that C++ didn't support.

The idea here is to build a software system in terms of readily understood concepts. The resulting system has its complexity in the *interactions* of simply understood things, rather than in the things themselves.

---

Note that I often write an interface when I intend only one implementing class. Capture the concept first.

Again, note that I often write an abstract class that has protocol (abstract methods) for every method in any intended subclass. This lets me capture a common implementation, but keeps my concept clear as I subclass. I never need to ask *instanceof* and I never need to cast an object reference. Everything naturally fits together freeing my mind to think of other (higher level) things.

> **Comments:**
> **username_5**: I would not expect a student to be able to implement an interface, before they have done classes, and inheritance. But they should be able to inherit an interface that you have made, and implement it. It would also be better to have a full interface (that is one with contracts). This way the student knows what to implement.  
> **username_1**: I like that you think in abstract first before coding, but I am surprised that you did not mention use of a different [paradigm](https://en.wikipedia.org/wiki/Programming_paradigm) when programming, e.g. functional or logic. I personally prefer logic, e.g. Prolog or Mercury, or other more abstract means of computation. The problem is that to much of the world is done with the old technology and sometimes using that for their exiting libraries outweighs the value in using another paradigm, e.g. [NumPy](http://www.numpy.org/)  
> **username_6**: While interfaces are generally developed before the implementing class; that doesn't mean you have to teach in in the same order. Interfaces are built with a consideration to the future (the implementation), which means the student needs to know what implementation entails. Interfaces are the solution to a problem, and therefore you must first showcase the problem to justify using the interface as a solution.  
> **username_4**: True enough, but interfaces are also good in the thinking stage of problem solving when you are deciding what it is you need. They are like a rough sketch of the capabilities needed.  
> **username_7**: I agree with @username_4 here, but I would add that the **idea** of 'Interfaces' be _**introduced**_ first as opposed to being necessarily _**taught**_ in  any order. It is very important for OO students to focus on the _behaviors_ of objects.  You are not required to actually use the terms when you teach the topics. ;)  

---

### Answer by username_8 on 2018-02-08. Score: 2

I'd say that it can fit very well between the Inheritance and the Polymorphism.
Interfaces are a way of promising that any implementing class supports the interface's functionality. for example, given an interface Movable:
public interface Movable {

    public void move();

}

one knows that anything that for any object instance of a class which implements Movable, we can call .move().
This means that, unlike extending a class, implements means (and directly implies) that instances of the class have some functionality. 
Teaching it in this order gives the view of an interface being nothing more than a promise for some functionality, and how such a thing is integrated into OOP. 
This also avoids some of the confusion I've witnessed in cases where abstract classes are taught before interfaces. They are not the same, but understanding why it's ok for something not to have a method body (i.e. abstract methods in such a class) is clearer to students if they know interfaces.
Additionally, if one teaches interfaces as a contract of functionality, then it doesn't appear as an add-on. It's a fully-fledged OOP concept with incredible usefulness. 
Whenever a person needs to describe just the functionality required by an unknown set of objects (and maybe of different classes), an interface is their best friend.


---

### Answer by username_2 on 2018-02-08. Score: 1

My laundry list:

We write sample programs using components from a library (say JavaFx, or SFML)
We don't care (much) about the internals of objects, as long as they work, but about their programming interface = what we can do with them. Abstraction.
Anyway, we're going to build new objects. Some explanations about classes/methods/fields. Encapsulation. 
Some details are better kept private. Enforcement of abstraction.
Objects are meant to be used through their API, so we first have to design the API. How we want innocent programmers to use them. Objects as service providers.
Enter the notion of (java) interface (sorry, C++ guys) as "contract", and classes realizing them.
Some examples of classes with the same interface. Polymorphism.
Some classes have lots in common, introduce abstract class to factorize code, and inheritance.
designing abstract classes in order to be a sound basis for concrete classes (some patterns).
sometimes, inheritance from a concrete class.

Students are exposed to polymorphism at a early stage: the example is a "guess a random number" game. The game is parameterized (?) by a Generator. During debugging phases a FakeGenerator is used, which always returns the same value.
public interface Generator {
   int getNumber();
}

public class FakeGenerator  implements Generator {
   final int fixedValue;
   public FakeGenerator(int value) {
       fixedValue = value;
   }
   @Override
   public int getNumber() {
       return fixedValue;
   }
}
class Game {
    Generator generator;
    Game(Generator generator) {
         this.generator = generator;
    }
...
}
Game game = new Game( 
                   // new UniformGenerator(1,10) // 
                      new FakeGenerator(33)  // for debugging
                    );


---

### Answer by username_3 on 2018-01-24. Score: 1

Interfaces should be taught right after, or during the section on APIs, because they are simply a promise to implement a certain Application Programming Interface. It's why they are called interfaces in the first place. They are also more general than inheritance, which makes them a much better example as a polymorphic data type. With this in mind, I propose that after APIs, the following concepts are taught in order:

1. **Interfaces:** This is language support for what was taught during APIs, and introduces the 'duck principle'. That is, if it walks like a duck, quacks like a duck, and behaves like a duck, its a duck. This is a good argument and explanation for the need for polymorphism.
2. **Polymorphism:** What good is an interface if we can't swap out one implementation for another? Take for example, Java's `List` interface, and two of its realizations: `LinkedList` and `ArrayList`; without polymorphism, we can't write generic code that targets both of these types.
3. **Abstract Classes:** Sometimes, when writing two different realizations of the same interface, we see that code can be shared between different types, while fundamental portions of the implementation remain separated. Take, for example, designing a `HumanPlayer`and `ComputerPlayer` for a card game. Functionality relating to scoring and storing cards will be shared, but the act of choosing which card to play is fundamentally different, and cannot be shared. The interface, however, needs to be consistent.
4. **Inheritance:** This is primarily for code reuse. Additional functionality or desired traits are added to a type that already is an object in its own right. Taking advantage of a somewhat nebulous implementation (such as extending Java's `Exception` class) is another use case. You are customizing more than you are providing functionality. Unlike the other strategies listed, you don't have to use inheritance to implement an API. You could, for example, convert a regular binary tree into a balancing binary tree.

Teaching these concepts in this order should help to showcase the use cases for each of these features. Interfaces were brushed over when I was learning programming, and it took me a while to understand what their use case was; I would try to use inheritance anywhere I needed polymorphism, which doesn't make sense in the grand majority of cases. This teaching order should get over that misconception.


---

