2
$\begingroup$

My colleague and I are considering bringing Greenfoot into our AP classes this year, and while Interfaces have been removed from the AP subset, we will continue to teach them.

Greenfoot itself seems to either not have, or to have thoroughly hidden, interfaces. However, we are not interested in reinventing the wheel if it's not necessary. Are there good lab activities that heavily feature interfaces already developed for Greenfoot?

Alternatively, are there any strong lab ideas for Greenfoot and interfaces that we could run with?

$\endgroup$
  • $\begingroup$ Interfaces are just a kludge in the java language, to allow polymorphism in the absence of multiple inheritance (The developers of Java did not know how to safely implement repeated inheritance, so eliminated multiple inheritance). $\endgroup$ – ctrl-alt-delor Sep 5 at 21:43
  • 1
    $\begingroup$ @ctrl-alt-delor I can't say as I agree with you there. Interfaces are more powerful than that, and used in plenty of places where any sort of inheritance would be inappropriate, such as iterators, or compareto. $\endgroup$ – Ben I. Sep 5 at 22:05
  • $\begingroup$ To make a class an iterator (or anything else): class A is an iterator. Is an example of polymorphism. (so exactly as un-powerful as I stated). And full inheritance is totally appropriate. It is just banned in Java, and can be dangerous in C++ (but only if repeated (not just multiple) inheritance). There are languages that make even repeated inheritance safe. $\endgroup$ – ctrl-alt-delor Sep 7 at 21:23
  • 1
    $\begingroup$ @ctrl-alt-delor I suppose we'll have to agree to disagree here. Interfaces are lightweight, versatile, and allow the compiler to do important work for you. Using inheritance to make your iterators seems like an odd idea. My n-ary tree, my 3-dimensional jagged array, and my spanning tree should implement Iterator completely differently. The only thing that my foreach loop needs to know is that they can iterate. I'm not aware of any language that is statically typed, but does not support interfaces (or abstract classes, which gets you the same thing). $\endgroup$ – Ben I. Sep 8 at 2:29
  • $\begingroup$ @ctrl-alt-delor Even Python added a module for abstract classes, and it's dynamically typed, so there is nominally no need for it. $\endgroup$ – Ben I. Sep 8 at 2:34
2
$\begingroup$

One huge advantage to using Greenfoot is the visualization and interactive nature of the application. Using and demonstrating interfaces will take some work though.

Anything that gets added to the world will be displayed and interacted with as it's actual type via the context menu. This can be confusing as students will be able to execute (via the context menu) all methods that are in its hierarchy: Object, Actor, ASubClass, etc. Even if the declared type is an interface.

On the other hand, when working programmatically the coder will be restricted to the methods defined by the declared type (the interface). This is very useful and worth demonstrating via code completion (ctrl+.) and compiler errors.

Using interfaces add clarity to the design and responsibility of classes in an application. This is a great lesson for students.

In a Greenfoot application most visible objects are Actors. In many applications programmers are making use of the World/Actor mechanism (by subclassing), yet these classes often have their own unique behavior and semantics that are separate and distinct from the Actor class and are only Actors to leverage the Greenfoot World/Actor architecture.

You will see these issues clearly if you create any scaffolded exercises or simulations of your own.

I use Conway's Game Of Life as a project for a few labs. The logic behind the simulation is very easy to understand and implement, and this allows the student to focus on design issues that interfaces help with. A cell can really only live or die; is represented by an Actor subclass; but should never be able to behave as one, ie move or rotate. See Demo.

Karel J Robot is another good simulator that can used for labs (also available to educators via GreenRoom), as can any tile-based game or application. The next demo is the introduction to interfaces lesson from the text book A Gentle Introduction to Object Oriented Programming with Karel J Robot where each contractor implements a Worker interface to build a part of the house. See Demo

$\endgroup$
  • $\begingroup$ Thanks for all of the detail! It sounds like Greenfoot isn't really designed to show Interfaces specifically, which was what I was beginning to suspect. How much starter code do you provide for GoL? $\endgroup$ – Ben I. Aug 27 at 0:52
  • $\begingroup$ There is actually very little required. The most difficult aspect is managing the state transition because all cells have to change simultaneously. So I show that piece in class. When we move the solution to a MVC pattern (the universe has a separate model to maintain the state), I provide array copying methods. A large part of the lab is working within the iterative incremental build cycle. The core algorithm is provided by the wiki site. $\endgroup$ – Mr Bradley Aug 27 at 1:11
  • $\begingroup$ As a side note: comparing the execution speed and profiles between the two implementations is my lead in to Big-O and algorithm efficiency (space and time). I use the Java profiler and the students find that pretty cool - memory usage, heap size and the speed improvement is phenomenal. $\endgroup$ – Mr Bradley Aug 27 at 1:16
2
$\begingroup$

Yes, Greenfoot certainly has interfaces. I doubt that they would take them out. The people who built greenfoot are likely as appalled as I am about the College Board action on interfaces. But there is no UI element that specifically adds a new interface to a project.

But they may not appear in the same place that most classes do in the sidebar. Look at the bottom of the list.

Use the menu option to create a "New Class". Edit the resulting template to change it to a legal interface. Compile and save. This was with v 3.5.

You can do the same with other "class-like" things such as enumerations. The compiler hiding behind greenfoot is all of Java.

You can also go to https://greenroom.greenfoot.org and ask such questions there (after joining).

Also, many of the labs that you find on both the greenfoot and greenroom sites will use interfaces.

$\endgroup$
  • $\begingroup$ Even though Greenfoot uses the BlueJ editor it has never had a "New Interface..." menu item. You must create a new class and change the class to interface, as Buffy says. I have tested this with all versions from 3.04 thru 3.6. $\endgroup$ – Mr Bradley Aug 26 at 20:40
  • $\begingroup$ You can also create an interface (or other) with an external editor and import it from that same menu. There is actually a way to use Eclipse as an external editor for greenfoot if you don't like the built in one. Edit in eclipse and run in greenfoot. Once set up it is pretty straightforward. $\endgroup$ – Buffy Aug 26 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.