Search Contact information
University of Cambridge Home Department of Engineering
University of Cambridge >  Engineering Department >  computing help

Java Course selected topics

This page covers selected Java topics (I/O, Exceptions, Interfaces and Graphics). Other pages of CUED java support include

I/O

In some Java books I/O (especially with files) isn't mentioned until rather late. There are many types of sources and targets for I/O, and Java tries to take account of widely varying types of machines in a tidy (but safe) way. In consequence, I/O code might initially be a little longer than in some other languages.

Exceptions

These are much the same as C++ exceptions except that Java has more standard exception types and is more rigorous about their use.

Interfaces

Unlike C++, Java doesn't support multiple inheritance - objects can be derived from only one parent. However, Java has interfaces so that otherwise unrelated objects can show that they support certain method calls.

An interface declares a list of publically available methods and constants, but doesn't state where (in what class) the implementation of the methods are. interfaces are created and used rather in the same way as classes are

   public interface Music {
      public static final int = 999;
      public abstract void play();
   }

   class Player extends Applet implements Music {
      public void play() {
      ...
A class can implement an interface (as in Program6) or several interfaces. If it does so it has to at least claim that it's supplying the implementation. Note that implementing an interface has some similarities with extending a class with abstract methods - in both situations the new class has to provide an implementation. However, extending a class implies inheriting its data structures and non-abstract methods too. Here's a simple example of using interfaces

interface legs {
  public abstract int number_of_legs();
};

interface wings {
  public abstract int number_of_wings();
};

class Pegasus implements legs, wings {
  public int number_of_legs() { return 4;}
  public int number_of_wings() { return 2;}
}


class Inter {
  public static void main(String[] args) {
    Pegasus p= new Pegasus();
    System.out.println("p has " + p.number_of_legs() + " legs and " + p.number_of_wings() + " wings.");
  }
}

Graphics toolkits use interfaces. For example, in the AWT, there's an interface called MouseListener which has methods that are called when a mouse-button's pressed, etc. A class that wants to respond to mouse activity could implement this interface but would then have to provide code for all the methods - mouseClicked(MouseEvent e), etc. An alternative is to extend the MouseAdapter abstract class. This already implements the MouseListener interface (providing null methods) so the programmer needs to provide code only for the methods that interest them.

Graphics

Java graphics uses concepts that are common in graphics programming (which is frequenty object-orientated) but if you're new to computer graphics the approach may seem alien. There are containers (e.g. Frames) whose job it is to contain components (e.g. Buttons). Components are usually visible and share a set of methods (e.g. setForeground()) to change their appearance. Rather than programmers directly controlling everything, they have to be prepared to relinquish control in various ways Graphics used to be created using AWT but the Swing components, which are part of Java's Foundation Classes (JFC), are beginning to take over. The following program creates a titled window which responds to resizing. Note how the program gives layout hints but doesn't specify fixed coordinates for objects.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Swing1 {
    public static void main(String s[]) {
        JFrame frame = new JFrame("Demo1");

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        JLabel myLabel = new JLabel("Just a demo");
        myLabel.setPreferredSize(new Dimension(175, 100));
        frame.getContentPane().add(myLabel, BorderLayout.CENTER);

        frame.pack();
        frame.setVisible(true);
    }
}
The following program does rather more, adding panels (which are both containers and components) and a file chooser.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Swing2 {
    public static void main(String s[]) {
        JFrame frame = new JFrame("Demo1");

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.out.println("Window closed");
                System.exit(0);
            }
        });
        JPanel panel1 = new JPanel();
        panel1.setLayout(new BorderLayout());
        frame.getContentPane().add(panel1, BorderLayout.NORTH);

        JPanel panel2 = new JPanel();
        panel2.setLayout(new BorderLayout());
        frame.getContentPane().add(panel2, BorderLayout.SOUTH);

        JLabel myLabel = new JLabel("centred");
        panel2.add(myLabel, BorderLayout.CENTER);
        JLabel myLabel2 = new JLabel("down");
        panel2.add(myLabel2, BorderLayout.SOUTH);

        //Create a file chooser
        final JFileChooser fc = new JFileChooser();
        
        //In response to a button click:
        int returnVal = fc.showOpenDialog(panel1);

        frame.pack();
        frame.setVisible(true);
    }
}
© Cambridge University Engineering Dept
Information provided by Tim Love (tpl)
(with help from various COs).
Last updated: Feb 2003