An exercise on classes and inheritance. What will be printed by this program?

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();
    }
}

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.