What changes will make this code compile?

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?

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

Explanation:
Using the private protected, instead of the private modifier, for the declaration of the one() method, would enable the two() method to access the one() method.



Leave a Reply 2

Your email address will not be published. Required fields are marked *


Jazz

Jazz

“C”

Instead of private on the one() method , we can use protected on one() method so that it can be accessible by two() method.

James

James

The Answer is C.

private members of a class cannot be accessed by any other class, even if the class that is trying to access that member is derived from that class, regardless of the package it is in.