Title: Objects First in Java?
 1Objects First in Java?
3rd LTSN-ICS One-Day Conference on the Teaching 
of Programming
David J. Barnes The University of 
Kent d.j.barnes_at_kent.ac.uk http//www.cs.kent.ac.u
k/djb/ 
 2What do I mean by objects first?
- An approach that focuses on the key features of 
 objects
- Objects have state. 
- Objects have behaviour. 
- An objects state can affect its behaviour. 
- An objects behaviour can affect its state. 
- Lets start with a journey ...
3A teaching-language journey
- Pascals domination in the 1980s. 
- C, C, ... as a second language. 
- The search for a replacement in the early 1990s. 
- We chose Modula 3. 
- Others chose ...? 
- Meanwhile, OO was gradually moving down through 
 the curriculum.
4The three (OO) bears
- In the late 1990s, it was time for another 
 change.
- Smalltalk was too niche. 
- C was too scary. 
- Java was just right. 
- Modern. 
- Applets (web). 
- Graphics. 
- Could be taught like Pascal.
5Main-method hello-world
public class MyFirstProgram  public static 
void main(String args)  
System.out.println("hello, world")   
 6Main-method input handling
import java.io. public class EarlyProgram  
 public static void main(String args) throws 
IOException  DataInputStream in  
new DataInputStream(System.in) int 
number  Integer.parseInt(in.readLine()) 
System.out.println(number  " squared  "  
 (number  number))   
 7Applet hello-world
import java.applet.Applet import 
java.awt.Graphics public class MyFirstApplet 
extends Applet  public void paint(Graphics 
g)  g.drawString("hello, world", 10, 
10)   
 8Applet input handling
public class EarlyApplet extends Applet 
implements ActionListener  private TextField 
input  new TextField(5) private Label 
result  new Label(" ") 
public void init()  
input.addActionListener(this) 
add(input) add(result)  
public void actionPerformed(ActionEvent ev) 
 int number  Integer.parseInt(input.getTex
t()) result.setText(number  " squared  " 
 (number  number))   
 9Where is the OO in this?
- Early examples are critical to the success of 
 teaching OO.
- None of these examples really addresses how to 
 teach OO.
- The design of Java doesnt really do us any 
 favours in this respect.
- We need help in overcoming Javas syntactic 
 barriers.
10Enter BlueJ
- BlueJ is a free interactive Java environment. 
- Designed specifically to support introductory 
 teaching of Java.
- Designed to support an objects-first approach. 
- Developed originally at Monash University by 
 Michael Kölling and John Rosenberg.
- Michael is now at the University of Southern 
 Denmark.
- Development work continues actively. 
- See http//www.bluej.org/
11BlueJ Demo 
 12Key issues from the demo
- We can introduce classes and objects from the 
 start, without having to stumble over syntax.
- Objects become real 
- We can interact with their methods. 
- We can inspect their state. 
- We can observe how their state changes in 
 response to method calls.
13Rethinking course structures
- Having started with real OO, we can rethink what 
 comes next.
- Textbooks are traditionally organised by language 
 construct
- Assignment, if statements, loops, arrays, etc. 
- We can make these topics the servants of an 
 objects-first approach rather than the masters.
14Some suggestions
- Emphasise the use of constructors for 
 initialising state.
- Introduce if-statements as a means to protect and 
 guarantee the state of an object.
- Introduce collections of objects early. 
- Use ArrayList or LinkedList in preference to 
 fixed-size arrays.
- Introduce while-loops as a means to iterate over 
 a collection.
15Use large (interesting!) examples
- Dont be afraid to use large examples. 
- Let students see the code. 
- Partition the code properly into separate 
 classes.
- Write and document the code well. 
- They will provide collateral learning 
 opportunities.
16Inheritance
- There is no need to introduce this very early. 
- Start by introducing inheritance purely as 
 extension/reuse.
- Javas extends word nicely suits this approach. 
- Consider introducing abstract classes in 
 association with method overriding.
- Where there is no obvious common behaviour 
 between sibling classes.
17Summary
- If we are going to use an OO language, it makes 
 sense to teach it using OO principles.
- We may still be labouring under the weight of the 
 Pascal heritage.
- An environment like BlueJ frees us from some of 
 Javas unhelpful syntax.
- A proper objects-first approach is possible.
18Acknowledgements
- Although I have played no part in the development 
 of the BlueJ environment, I have been fortunate
 to work closely with both Michael Kölling and
 John Rosenberg. My thinking in this area has
 clearly been strongly influenced by their work.