<p>I have used an example of a <code>machine</code> which has a serial number and a state. It can be turned on and off, it can be told to give out its serial number. A <code>bMachine</code> can be powercycled (inheritance from <code>machine</code>). There is a <code>machinedesk</code> which contains <code>machines</code>. They can self check.</p>

<p>I worked through a whole range of 'things' that the objects could do to demonstrate everything in the AQA A-level spec in Python. I then got students to take what I had demonstrated and build joke machines.</p>

<p>The purpose of this is the <strong>context</strong> - students struggle to see why on earth you would model an animal whilst they can see a simple extension to allow these objects to control actual machines.</p>

CC BY-SA 3.0
2017-06-06T16:51:45.733Z
2017-06-06T17:49:02.760Z
2017-06-06T17:49:02.760Z
Oli Howson
<p>You could have different implementations of a bit of hardware, e.g. printer. </p>

<p>Or a concept such as sorting. sort ⇐ bubble sort, sort ⇐ merge sort, sort ⇐ insertion sort, sort ⇐ quick sort, sort ⇐ a hybrid sort, …</p>

<p>A stack implemented with an array and with a linked list.</p>

<p>A queue implemented with an array (circular buffer) and with a linked list.</p>

<hr>

<p>Objects are not always objects. We need to remember and teach, that not all Objects (in Object orientation), are objects. Some are algorithms (The sort algorithm, so still a noun ).</p>

CC BY-SA 3.0
2017-06-06T20:46:57.073Z
2017-06-06T20:52:13.077Z
2017-06-06T20:52:13.077Z
ctrl-alt-delor
<p>One example that isn't necessarily exciting but would allow for a lot of class design is a <code>Shape</code> interface. From there let's say you have <code>Quadrilateral</code>, <code>Ellipse</code>, and <code>Triangle</code> as classes which implement <code>Shape</code>. You could do some really cool things by including as methods formulas for area and perimeter.</p>

<p>Additionally, within <code>Quadrilateral</code>, you could further subclass into <code>Parallelogram</code> (and further with <code>Rhombus</code> and <code>Rectangle</code> and <code>Square</code>?), <code>Trapezoid</code>, and <code>Kite</code>. Students will clearly see the relationship of <code>Shape -&gt; Quadrilateral -&gt; Parallelogram -&gt; Rectangle -&gt; Square</code>. This will also hammer home the ideas that a subclass IS-A class as in a square IS-A rectangle and that the inverse (a class IS-A subclass) is not true.</p>

<p>This can lead to fun, but important design conversations. Do you indeed make a subclass for <code>Square</code>, or do you have an <code>isSquare()</code> method on <code>Rectangle</code> testing for equality of length and width?</p>

<p>A second example is this: give the students this challenge. Just as we are coming up with examples of how to categorize objects with IS-A relationships, so too should students come up with their own system of inheritance. I would bet that they would come up with some pretty inventive ideas. Moreover, the power of class design in OOP will "stick" if they come up with the system from the ground up.</p>

CC BY-SA 3.0
2017-06-04T06:24:44.430Z
I very much like the idea of asking them to be creative. Thanks!
ItamarG3
CC BY-SA 3.0
2017-06-22T00:39:29.163Z
I like to use the example of Square inheriting from Rectangle vs inheriting from Shape.  What happens to a Square (with a given Side length) if you set Width different from Height?  After working it out, it shows that inheritance should have one responsibility - to fit the base interface Shape.  It doesn't always make sense for types to derive from each other.  Think Liskov Substitution Principle.
John Deters
CC BY-SA 3.0
2017-06-04T00:20:31.333Z
2017-06-04T00:20:31.333Z
Peter
<p>You could have a hierarchy based on an abstract class or interface <code>Car</code>, with implementations/subclasses for <code>ElectricCar</code>, <code>HybridCar</code>, <code>GasBurningCar</code>, etc. This illustrates that there can be very different implementations for an interface.</p>

<p>Each class would have different additional members. For example, a <code>GasBurningCar</code> could have a maximum amount of gas (in gallons or liters), represented as an <code>int</code> or <code>double</code>, or it could have a field of type <code>GasTank</code>. Having members gives students practice with has-a relationships, in addition to is-a relationships.</p>

<p>You could discuss trade-offs in whether an individual car model, such as Prius, should be an instance of <code>HybridCar</code> or a subclass.</p>

CC BY-SA 3.0
2017-06-03T16:42:41.420Z
This is an interesting Idea to show them the is-a and has-a relationships. For some reason, students sometimes get confused with those
ItamarG3
CC BY-SA 3.0
2017-06-03T16:36:06.083Z
2017-06-03T16:36:06.083Z
Ellen Spertus
<p>After teaching inheritance and implementation (<code>implements</code> vs <code>extends</code> in Java) and overriding methods and basic OOP (classes and interfaces), as well as polymorphism (up/downcasting) I want to give the students some exercises to practice this. </p>

<p>Currently, I have the famous <code>Animal -&gt; Mammal</code> example, as well as <code>Human -&gt; Teacher</code>, <code>Human -&gt; Student</code>.</p>

<p>I am trying to think of new examples and exercises to practice the subjects mentioned. To give a better notion of what I'm looking for, the <code>Animal</code> exercise looks like this:</p>

<pre><code>public abstract class Animal implements AnimalActions {
    private String name, family;
    private double weight;

    public Animal(String name, String family, double weight) {
        this.name = name;
        this.family = family;
        this.weight = weight;
    }
}

interface AnimalActions {
    public void eat();

    public void sleep();

    public void makeSound();
}
</code></pre>

<p>And then:</p>

<pre><code>public class Mammal extends Animal {
    public Mammal(String name, String family, double weight) {
        super(name, family, weight);
    }

    @Override
    public void eat() {

    }

    @Override
    public void sleep() {

    }

    @Override
    public void makeSound() {

    }
}
</code></pre>

<p>This covers many things and is useful for teaching. However, this doesn't go very far if I make it into a practice exercise. What exercises do others give students in this subject? </p>

<p>The students are in high school.</p>

CC BY-SA 3.0
2017-06-03T15:44:07.900Z
Have you covered polymorphism with them at this point? (`ArrayList<Animal> a = new ArrayList<Animal>(); a.add(new Mammal());`)?
Ben I.
CC BY-SA 3.0
2017-06-03T15:46:11.703Z
@BenI. Yes. I forgot to write that. I'll edit to include it
ItamarG3
CC BY-SA 3.0
2017-06-03T16:38:22.807Z
Consider changing title to be more in line with other titles.
Ellen Spertus
CC BY-SA 3.0
2017-06-05T13:38:54.077Z
My classes do a lot with GUI/Event-Driven programming because this is a very natural realm in which to use inheritance.
ncmathsadist
CC BY-SA 3.0
2017-06-22T17:56:22.870Z
If you don't mind, I'm going to link the [answer I just supplied on another question](https://cseducators.stackexchange.com/questions/146/what-is-a-good-analogy-for-the-object-oriented-paradigm/970#970).
Draco18s no longer trusts SE
<lesson-ideas><object-oriented-paradigm><exercises>
Basic OOP practice exercises for inheritance and polymorphism
# Basic OOP practice exercises for inheritance and polymorphism

(Asked by: username_0 on 2017-06-03)

After teaching inheritance and implementation (implements vs extends in Java) and overriding methods and basic OOP (classes and interfaces), as well as polymorphism (up/downcasting) I want to give the students some exercises to practice this. 
Currently, I have the famous Animal -> Mammal example, as well as Human -> Teacher, Human -> Student.
I am trying to think of new examples and exercises to practice the subjects mentioned. To give a better notion of what I'm looking for, the Animal exercise looks like this:
public abstract class Animal implements AnimalActions {
    private String name, family;
    private double weight;

    public Animal(String name, String family, double weight) {
        this.name = name;
        this.family = family;
        this.weight = weight;
    }
}

interface AnimalActions {
    public void eat();

    public void sleep();

    public void makeSound();
}

And then:
public class Mammal extends Animal {
    public Mammal(String name, String family, double weight) {
        super(name, family, weight);
    }

    @Override
    public void eat() {

    }

    @Override
    public void sleep() {

    }

    @Override
    public void makeSound() {

    }
}

This covers many things and is useful for teaching. However, this doesn't go very far if I make it into a practice exercise. What exercises do others give students in this subject? 
The students are in high school.

**Tags:** lesson-ideas, object-oriented-paradigm, exercises

> **Comments:**
> **username_1**: Have you covered polymorphism with them at this point? (`ArrayList<Animal> a = new ArrayList<Animal>(); a.add(new Mammal());`)?  
> **username_0**: @BenI. Yes. I forgot to write that. I'll edit to include it  
> **username_2**: Consider changing title to be more in line with other titles.  
> **username_3**: My classes do a lot with GUI/Event-Driven programming because this is a very natural realm in which to use inheritance.  
> **username_4**: If you don't mind, I'm going to link the [answer I just supplied on another question](https://cseducators.stackexchange.com/questions/146/what-is-a-good-analogy-for-the-object-oriented-paradigm/970#970).  

---

## 4 answers

### Answer by username_7 on 2017-06-04. Score: 2 | Accepted answer

One example that isn't necessarily exciting but would allow for a lot of class design is a `Shape` interface. From there let's say you have `Quadrilateral`, `Ellipse`, and `Triangle` as classes which implement `Shape`. You could do some really cool things by including as methods formulas for area and perimeter.

Additionally, within `Quadrilateral`, you could further subclass into `Parallelogram` (and further with `Rhombus` and `Rectangle` and `Square`?), `Trapezoid`, and `Kite`. Students will clearly see the relationship of `Shape -> Quadrilateral -> Parallelogram -> Rectangle -> Square`. This will also hammer home the ideas that a subclass IS-A class as in a square IS-A rectangle and that the inverse (a class IS-A subclass) is not true.

This can lead to fun, but important design conversations. Do you indeed make a subclass for `Square`, or do you have an `isSquare()` method on `Rectangle` testing for equality of length and width?

A second example is this: give the students this challenge. Just as we are coming up with examples of how to categorize objects with IS-A relationships, so too should students come up with their own system of inheritance. I would bet that they would come up with some pretty inventive ideas. Moreover, the power of class design in OOP will "stick" if they come up with the system from the ground up.

> **Comments:**
> **username_0**: I very much like the idea of asking them to be creative. Thanks!  
> **username_8**: I like to use the example of Square inheriting from Rectangle vs inheriting from Shape.  What happens to a Square (with a given Side length) if you set Width different from Height?  After working it out, it shows that inheritance should have one responsibility - to fit the base interface Shape.  It doesn't always make sense for types to derive from each other.  Think Liskov Substitution Principle.  

---

### Answer by username_2 on 2017-06-03. Score: 3

You could have a hierarchy based on an abstract class or interface `Car`, with implementations/subclasses for `ElectricCar`, `HybridCar`, `GasBurningCar`, etc. This illustrates that there can be very different implementations for an interface.

Each class would have different additional members. For example, a `GasBurningCar` could have a maximum amount of gas (in gallons or liters), represented as an `int` or `double`, or it could have a field of type `GasTank`. Having members gives students practice with has-a relationships, in addition to is-a relationships.

You could discuss trade-offs in whether an individual car model, such as Prius, should be an instance of `HybridCar` or a subclass.

> **Comments:**
> **username_0**: This is an interesting Idea to show them the is-a and has-a relationships. For some reason, students sometimes get confused with those  

---

### Answer by username_5 on 2017-06-06. Score: 2

I have used an example of a `machine` which has a serial number and a state. It can be turned on and off, it can be told to give out its serial number. A `bMachine` can be powercycled (inheritance from `machine`). There is a `machinedesk` which contains `machines`. They can self check.

I worked through a whole range of 'things' that the objects could do to demonstrate everything in the AQA A-level spec in Python. I then got students to take what I had demonstrated and build joke machines.

The purpose of this is the **context** - students struggle to see why on earth you would model an animal whilst they can see a simple extension to allow these objects to control actual machines.


---

### Answer by username_6 on 2017-06-06. Score: 1

You could have different implementations of a bit of hardware, e.g. printer.

Or a concept such as sorting. sort ⇐ bubble sort, sort ⇐ merge sort, sort ⇐ insertion sort, sort ⇐ quick sort, sort ⇐ a hybrid sort, …

A stack implemented with an array and with a linked list.

A queue implemented with an array (circular buffer) and with a linked list.

---

Objects are not always objects. We need to remember and teach, that not all Objects (in Object orientation), are objects. Some are algorithms (The sort algorithm, so still a noun ).


---

