mercoledì 27 marzo 2013

Q23

Q23


Given a java source file:
class x {
       x() {
       }

       private void one() {
       }
}

public class Y extends x {
       Y() {
       }

       private void two() {
             one();
       }

       public static void main(String[] args) {
             new Y().two();
       }
}
What changes will make this code compile?
A.
adding the public modifier to the declaration of class x
B.
adding the protected modifier to the x() constructor
C.
changing the private modifier on the declaration of the one() method to protected
D.
removing the Y () constructor
E.
removing the private modifier from the two () method
La risposta è C.
Il codice non compila perchè Y non riesce a vedere il metodo one, essendo questo privato e limitato solo alla classe X. Cambiando il modificatore di one a protetto Y riesce a vederlo.


Riferimento : modificatori
 

Nessun commento:

Posta un commento