On Object oriented programming pedagogy

It's the time of the year where we're thinking about 'September Readiness' and what the next academic year will look like. Since one of the first topics I'll be teaching year 13 in September is the basics of object oriented programming, I've been reflecting on how this can be done most effectively.

My learning experience

Despite first encountering OOP at A-Level, I didn't truly understand the paradigm until taking a data structures module at university. Throughout that time OOP had been presented as an idea to me in a range of different ways: animals and vehicles examples, (slightly more usefully) a JavaFX application and finally through the implementation of data structures. 

In the rest of this post I'm going to attempt to explain why I think only the last of those really worked and why I think it is the most viable approach to use to introduce OOP to A-Level students. 

A different way of programming?

I've seen lots of teaching resources describe OOP in this way and it is true. It is different to other programming paradigms. However it's important I think to consider to what extent this is actually the case for your students. It will be different in some ways: creating a class, defining fields and creating methods, for example, will all be new.

Nevertheless there will be some things that probably won't be new. For example, if your students have been learning in Python at AS, they'll have probably used methods of string objects such as split(), isalpha(), for example, so they have some prior knowledge of the paradigm. 

The problems with Animals and Vehicles 

I get where this comes from. In teaching it is often useful to relate the concept we're trying to teach to examples from student's prior knowledge. Students are very likely to understand that there are different types of animal which can share characteristics and different types of vehicle that share characteristics. 

However, when considering whether it is appropriate to use an example in your teaching it's important to consider how abstract the example is. If it's too abstract students will struggle to relate the learning to situations in which they're asked to apply that knowledge. Students will almost certainly never have to write a Animal or Vehicle inheritance hierarchy in any other situation but they will certainly at least make use of data structures and possibly be required to write/modify/extend them.   

The JavaFX application I mentioned earlier was closer to being the right next step but I was missing prior knowledge (about how GUIs work) so the example was not as useful as it could've been as I struggled to directly relate what was happening on screen to the code I was writing.   

Favour composition over inheritance

The specification for the course that I teach states that students should:
Be aware of the following object-oriented design principles:
  • encapsulate what varies
  • favour composition over inheritance
  • program to interfaces, not implementation.

The animals/vehicles approach to teaching OOP not only overlooks the second of these points, it actively teaches students to do the opposite and favour inheritance over composition. It's artificially creating an example that requires a lot of inheritance which leads students to develop the habit of defaulting to creating complicated inheritance hierarchies when they're not needed. So it's worse than ineffective, it's introducing and embedding a common misconception. 

A different approach

I think introducing OOP through the use of common data structures as examples is so much better because it builds on the programming that students already know, rather than introducing the topic as this scary, difficult, new thing that requires a "whole new mindset". 

This topic is a great case study in making sure your planning begins with thinking "what do I want my students to know" rather than "what activities can I use". Being able to critically assess whether a resource/activity/approach is right for the students in front of you is critical to delivering the most effective learning experience. Knowing where students are at and responding to that rather than blindly following approaches that have been used before will help your students make the most progress.