LARA

Exercises 07

Exercise 1

Run javac on the following program:

class A { }
class B extends A { void foo() { } }
class Test {
    public static void main(String[] args) {
	B[] b = new B[5];
	A[] a;
	a = b;
	System.out.println("Hello,");
	a[0] = new A();
	System.out.println("world!");
	b[0].foo();
    }
}

Explain what happened.

Then, run

java -cp . Test

Explain what happened.

Exercise 2

If the following programs type-check according to the rules given in the course, give the corresponding type derivation tree, otherwise give a partial tree that shows where it doesn't work

a)

class A {
}

class B extends A {
}

class Test {
  val array: Array[A] = new Array[B](2)
  array(0) = new A
  array(0) = new B	
}

b)

class Test {
  class A {}
  class B extends A {}
  class C extends B {}
  def func(x: B=>B): A
  val y: A=>C
  val z = func(y)	
}