Over a million developers have joined DZone.

Java Quiz: Extending an Abstract Class

The new quiz that tests your Java skills! Every Thursday, only on DZone.

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

Last week's quiz asked you what happens when you try to compile the following program:

class Outer {
    private int a = 12;
    private int b = 6;
    Outer() {
        NestedA nestedA = new NestedA(4);
        a = a -12;
    }
    class NestedA {
        NestedA(int y){
            NestedB nestedB = new NestedB(y);
            nestedB.methodB(y, a);
            a= a + b - y;
        }
        private void methodA(int z) {
            System.out.print("-a"+a+z+"-b"+(b-z));
        }
        class NestedB {
            NestedB(int i) {
                a = a - i; b = b +i;
            }
            private void methodB(int x, int z) {
                if(x < 5) {
                    a = a+x+b;
                }
                b = b+z; 
                System.out.print("-a"+a+"-b"+b);
            }
        }
    }
    public static void main(String[] args) {
        NestedA nestedA = new Outer().new NestedA(2);
    }
}

Over 1,800 of you responded, and if you're one of the 24.3% that got it right, then you know the answer is that the program writes "-a22-b18-a44-b42" to the standard output.

The following steps help to understand the right answer:

a = 12, b = 6 

1. Calling the Outer no-arg constructor

new Outer()
          new NestedA(4)            // calling the NestedA int-arg constructor
                  new NestedB(4)    // calling the NestedB int-arg constructor
                          a = a - i = 12-4 = 8;
                          b = b+i = 6+4 = 10;
                  nestedB.methodB(4, 8)
                           x = 4, z = 8;
                           if(4 < 5) = true
                           a = a + x + b = 8 + 4 + 10 = 22;
                           b = b + z = 10 + 8 = 18;
                           printing the first part (-a22 – b18);
                  a = a + b -y = 22 + 18 – 4 = 36;
    a = a -12 = 36 -12 = 24;

2. Calling the NestedA int-arg constructor

 new NestedA (2)         // calling the NestedA int-arg constructor
           a = 24, b = 18,    // The last values of a and b
           new NestedB(2)   // calling the NestedB int-arg constructor
                  a = a - i = 24 – 2 = 22;
                  b = b + i = 18 + 2 = 20;
           nestedB.methodB (2, 22)
                  if(2 < 5) = true
                  a = a + x + b = 22 + 2 + 20 = 44
                  b = b + z = 20 + 22 = 42
                  printing the second part (-a44-b42)

The result is: “-a22-b18-a44-b42”

Now, for Sars' next question:

What is written to the standard output as the result of executing the following code?

public abstract class MyAbstract {
    int x = 3;
    public MyAbstract() {
        x *= 2;
        System.out.print("-x" + method(3));
    }
    abstract int method();
    abstract int method(int i);
}

public class MySuper extends MyAbstract {
    int x = 2;
    public MySuper() {
        System.out.print("-x" + method());
    }
    int method() {
        x++;
        return (x * 5 + super.x);
    }
    int method(int i) {
        return (method() + i);
    }
}

public class MySub extends MySuper {
    MySub() {
        x += 3;
        System.out.print("-x" + x);
    }
    int method() {
        return x + 5;
    }
    public static void main(String[] args) {
        MySub sub = new MySub();
    }
}

You can answer the question below:


As always, please send any feedback you have about our new series to content@dzone.com! 

Check out Sar's site here!

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

Topics:
java ,quiz

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}