15.01.2015 Views

4th International Conference on Principles and Practices ... - MADOC

4th International Conference on Principles and Practices ... - MADOC

4th International Conference on Principles and Practices ... - MADOC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

• Students c<strong>on</strong>sume genericity when they first use collecti<strong>on</strong>s<br />

from the Java Collecti<strong>on</strong> Framework, which may be<br />

relatively early <strong>on</strong>. Defining the element type of a<br />

collecti<strong>on</strong> is quite straightforward <strong>on</strong>ce the c<strong>on</strong>cept of a<br />

type is understood. Producing a generic class, <strong>on</strong> the other<br />

h<strong>and</strong>, requires much more knowledge about genericity; this<br />

can be taught much later.<br />

We applied this principle in these <strong>and</strong> in several more situati<strong>on</strong>s<br />

in SD1. We will now turn to this course in more detail.<br />

4. TEACHING INTERFACES BEFORE<br />

INHERITANCE<br />

In this secti<strong>on</strong> we describe the structure of SD1 in as much detail<br />

as is necessary to point out our objective: to dem<strong>on</strong>strate that<br />

teaching interfaces before inheritance can be d<strong>on</strong>e<br />

systematically.<br />

We follow an Objects First approach in SD1, as described for<br />

example in [2]. Using BlueJ [11], students work with objects from<br />

day <strong>on</strong>e. BlueJ is a free IDE tailored for teaching object-oriented<br />

programming that we have been using in introductory<br />

programming courses for several years now. Instances of classes<br />

can be created interactively; any of their methods can be called<br />

interactively <strong>and</strong> their states can be inspected easily. The classes<br />

of a project are visible all the time in an interactive class diagram.<br />

4.1 Interfaces Early<br />

In their first programming tasks, students just create objects of<br />

provided classes. They interact with these objects, calling the<br />

public methods defined by the given classes. For our discussi<strong>on</strong>,<br />

the class editor’s ability to show two different views of a class<br />

definiti<strong>on</strong> is an important feature of BlueJ: the implementati<strong>on</strong><br />

<strong>and</strong> the interface of the class (generated <strong>on</strong> the fly using javadoc).<br />

During preparati<strong>on</strong> of the exercises, we save the class definiti<strong>on</strong>s<br />

of the provided classes in interface view. When students want to<br />

find out about the services a class offers, they just double-click <strong>on</strong><br />

the class symbol <strong>and</strong> the interface view is presented. Later,<br />

students start to extend the given class definiti<strong>on</strong>s <strong>and</strong> switch to<br />

the implementati<strong>on</strong> view then.<br />

While interactively exploring <strong>and</strong> extending the services of<br />

provided classes in the first weeks, students get an intuitive<br />

feeling for the interface of a class early <strong>on</strong>. During this time we<br />

do not make the distincti<strong>on</strong> between methods <strong>and</strong> operati<strong>on</strong>s, as<br />

this would hinder more than help the learning process. The<br />

vocabulary in use thus c<strong>on</strong>tains primarily the words class, object,<br />

instance, method, <strong>and</strong> field. The field types of the classes in use<br />

are primitive types <strong>on</strong>ly in the first four weeks.<br />

In week 5 we explicitly introduce the c<strong>on</strong>cept of reference types.<br />

We set reference types in relati<strong>on</strong> to the primitive types <strong>and</strong><br />

explain the noti<strong>on</strong> of a general type as described in secti<strong>on</strong> 3.3,<br />

primarily to explain static type checking in Java.<br />

4.2 Interfaces Next<br />

In week 7 we introduce interfaces together with unit testing.<br />

We explain that a black box test should just test the interface of a<br />

class. We then show how they can use the Java mechanism<br />

interface to explicitly describe the interface of a class. If a<br />

test class is just using this interface it can be expected that<br />

the test cases will treat the object at test really as a black box. In<br />

the test class, any variable should be declared of the interface<br />

type, not of the c<strong>on</strong>crete class type. The object to test is passed in<br />

as a c<strong>on</strong>structor parameter, even this parameter can be of the<br />

interface type. As BlueJ allows passing objects interactively, the<br />

object to test can be created interactively <strong>and</strong> passed interactively<br />

to a test class object.<br />

This introducti<strong>on</strong> of interfaces automatically leads to the<br />

distincti<strong>on</strong> of the static <strong>and</strong> the dynamic type of a variable. The<br />

static type of the variables should be of the interface type, the<br />

dynamic type is the c<strong>on</strong>crete class type. This way, students<br />

c<strong>on</strong>sume dynamic binding without the necessity to underst<strong>and</strong> the<br />

mechanics behind the scenes.<br />

After writing the black box test class, we introduce JUnit as a<br />

framework for better test support. Using JUnit, students also<br />

c<strong>on</strong>sume inheritance (they passively inherit methods of the JUnit<br />

class TestCase that they can use for asserti<strong>on</strong>s), but they need<br />

not produce an inheritance relati<strong>on</strong>ship (provide an abstracti<strong>on</strong><br />

that can be reused).<br />

4.3 Collecti<strong>on</strong> Interfaces<br />

The sec<strong>on</strong>d half of the semester is arranged around a central<br />

theme: collecti<strong>on</strong>s of objects. Again we take a different approach<br />

than most text books: we introduce two basic collecti<strong>on</strong><br />

interfaces of the Java Collecti<strong>on</strong> Framework (JCF), namely<br />

Set <strong>and</strong> List, before we introduce arrays (see [20] for<br />

supporting arguments for this approach). We explain the<br />

interfaces <strong>and</strong> let the students c<strong>on</strong>sume these types (<strong>and</strong> their<br />

implementati<strong>on</strong>s) to solve simple tasks with collecti<strong>on</strong>s. At this<br />

point we explicitly do not talk about the type hierarchy of the<br />

JCF. As menti<strong>on</strong>ed in secti<strong>on</strong> 3.5, students have to c<strong>on</strong>sume<br />

genericity without a full introducti<strong>on</strong> of the c<strong>on</strong>cept.<br />

For the rest of the semester we “open the hood” of the collecti<strong>on</strong><br />

abstracti<strong>on</strong>s <strong>and</strong> show how lists can be implemented with linked<br />

lists <strong>and</strong> “growing” arrays, <strong>and</strong> how sets can be implemented<br />

efficiently with hashing. In the exercises, the students have to<br />

build these implementati<strong>on</strong>s for simple (n<strong>on</strong>-generic)<br />

interfaces such as ShortList (for short lists in the sense<br />

of Nick Hornby’s “High Fidelity”) <strong>and</strong> Vocabulary (a set of<br />

strings for analyzing the corpus of a digital text). Arrays are<br />

described as a (very necessary) low-level c<strong>on</strong>struct <strong>on</strong>ly that is<br />

used to implement more user-friendly collecti<strong>on</strong>s.<br />

In summary, we use collecti<strong>on</strong> types as prime examples for type<br />

abstracti<strong>on</strong> (secti<strong>on</strong> 5.3), without menti<strong>on</strong>ing the term explicitly.<br />

4.4 Students Feedback<br />

SD1 was very well received by the students. In the “official”<br />

informal surveys c<strong>on</strong>ducted by the student uni<strong>on</strong>, the module got<br />

excellent marks, especially for its clear structure (over 80% voted<br />

“very good” or “good”). Students were quite aware of the fact that<br />

a different path was being taken with respect to interfaces,<br />

some were even excited about the novel approach. No student<br />

complained about the fact that inheritance was not taught <strong>and</strong> no<br />

student had problems with using interfaces the way we proposed.<br />

5. ADVANCED TERMINOLOGY<br />

The inheritance mechanisms in programming languages support<br />

several inheritance c<strong>on</strong>cepts. For first year teaching, the most<br />

important c<strong>on</strong>cepts are:<br />

205

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!