?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

Inheritance: Method Overloading/Method Overriding

APCSA-NKEWKP

Consider the following classes.

    public class A 
    {
        private int num;

        public A(int n)
        {
            num = n;
        }

        public int getNum()
        {
            return num;
        }

        public void setNum(int n)
        {
            num = n;
        }

        // other data and methods not shown
    }

    public class B extends A 
    {
        public B(int n)
        {
            super(n);
        }

        public double getNum(int n)
        {
            return super.getNum()*10;
        }

        public void setNum(double n)
        {
            if (n <= getNum())
                setNum(n*2);
            else
                super.setNum((int) n);
        }

       // other data and methods not shown
    }

What is the output to the console after the following segment of code from the main method is executed?

    A num1 = new A (50);
    B num2 = new B (40);
    B num3 = new B (30);

    num1.setNum(30);
    num2.setNum(10);
    num3.setNum(10.0);
    System.out.println(num1.getNum() + num2.getNum() + num3.getNum());
A

50

B

60

C

70

D

80

E

90