Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Quiz: Extending an Abstract Class

DZone's Guide to

Java Quiz: Extending an Abstract Class

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

· Java Zone
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

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!

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

Topics:
java ,quiz

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}