Does the following program compile? If not, which statements need to be commented out for it to compile?

What happens when the resulting program runs? Draw the memory picture to explain your answer.



public class Exercise {
    public static void main(String [] args) {
	A a = new A();
	B b = new B();
	C c = new C(10);

	a.m(3);
	b.m(3);
	c.m(3);
	
	a.print();
	b.print();
	c.print();

	a = c;
	a.m(1);
	c = (C) a;
	c.print();
    }
}

class A {
    private int x;

    // constructor 
    public A() {
	x = -10;
    }

    public void m (int y) {
	x = x + y;
    }

    public void print() {
	System.out.println("x = " + x);
    }
}


class B extends A {
    protected int y = 0;

    public void m(int z) {
	y = y + z;	
	super.m(y);
    }
}


class C extends B {
    private int z;

    public C(int w) {
	z = w;
	y = 5;
    }  

    public void print() {
	super.print();
	System.out.println("y = " + y + " z = " + z);
    }   
    
}

This is an example from CSci 2101 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.