\documentstyle{article}[12pt,a4]

\title{Supervision Questions\\Chapters 1-4}
\author{Matthew Johnson\\Trinity Hall\\mjj29@cam.ac.uk}

\begin{document}
\maketitle

{\bf 2-1}
\par
\begin{verbatim}
class Integer
{
   Integer(int i)
   {
      value = i;
   }
   int value;
}

....

Integer int1 = new Integer(8);
Integer int2 = new Integer(53);
System.out.println(int1.value);

\end{verbatim}

A {\em class} is a definition of a type of object, and says how any instance of that class would behave, what interfaces it has, what data it can store, and the definition of the methods that operate on its data, encapsulated into one item. In this case {\tt Integer} is a simple class that just holds one value.
To actually use a class (except in the case of {\tt static} items) you need to create an instance of the {\em object} it represents. The call {\verb\new Integer(8)\} creates an object which is an instance of the class {\tt Integer} and initializes it using the constructor with the value 8.
The identifiers {\tt int1} and {\tt int2} are {\em object references}. They are pointers in memory to the location of the objects that they refer to. Objects in Java cannot be accessed or instantiated directly, but must be accessed via object references (in contrast to C++ or Pascal, which differentiate between static and dynamic memory allocation).
The {\em type} of a variable refers to a definition of the storage of the variable, and the operations you can do on it. You declare the type when you instantiate the variable. For example, the type of {\tt value} in the example is {\tt int} and the type of {\tt int1} is {\tt Integer}.

\vspace{24pt}

{\bf 2-2}
\par
If {\em s} is a sub-type of {\em t}, that is in Java, if s {\tt extends} t, then it can be used wherever something of type {\em t} can be used. This is possible, because Java performs dynamic method invocation, so when you access a method of something of type {\em t}, it checks what object the reference is referring to, and looks up the actual type of that object. It then invokes the method from the class that the object was actually instantiated from.

\vspace{24pt}

{\bf 2-3}
\par

The syntax {\tt super.super} isn't valid in Java. This isn't too much of an issue, because the typical use of the super keyword, is when extending methods from a class you are inheriting from. When you override a method by default it replaces the original. If you want to merely extend it, then you call {\tt super}, and then add in the extra code. You rarely need to use this to call methods from anything other than your immediate parent class.
For accessing fields, you can simulate multiple {\tt super} calls by casting {\tt this} to the appropriate type, and then accessing the field. 
The advantages of disallowing {\tt super.super} are mainly that you don't have to check that it is a valid class. Since everything extends at least {\tt Object}, then {\tt super} is always valid, but {\tt super.super} isn't necessarily.


\vspace{24pt}

{\bf 3-2}
\par

Abstract classes are very similar to interfaces in Java, and can often be used in similar places. However, there are several difference. Abstract classes can have any number of their methods implemented in the abstract class, even all of them. A class extending them only has to provide implementations for the methods that the abstract class doesn't implement itself. Interfaces, on the other hand, have no implementations. This may seem like a case for only using abstract classes, but since Java doesn't allow multiple inheritance, you can only inherit from one class. You can, however, implement as many interfaces as you like. This trade off has helped the Java language from not allowing clashes of implementations of methods, but does slightly restrict what you can do.

\pagebreak

{\bf 3-5}
\par

The following Java program implements the given example using an anonymous inner class.

\begin{verbatim}
interface Closure
{
   public void apply ();
}

class test
{   
   static Closure myCounter (final int start)
   {
      return new Closure () {
         int counter = start;
         public void apply ()
         {
            System.out.println (counter++);
         }
      };
   }
   
   public static void main (String [] args)
   {
      Closure count = myCounter (5);
      for (int i = 1; i <= 6; i++) count.apply ();
   }
}
\end{verbatim}

When compiled and run you get the following output:

\begin{verbatim}
mjj29@binford:~/Docs/personal/cambridge/CompSci/csaa$ java test
5
6
7
8
9
10
\end{verbatim}

\end{document}




