SCJP Module-2 Question-11


 

SCJP Module-2 Question-11

The code given below checks the concepts of packages in java and prepares for the SCJP examination.

The code given below checks the concepts of packages in java and prepares for the SCJP examination.

Which modifier should  used in place of  **** , so that it's subclass saved in other package can access " FixVariable() " method of it's super class ?

package NewPackage;
public class Example extends rose{
private int length;
*** void FixVariable( int a ) {
length = a;
}
}


// subclass in different package


import NewPackage.Example ;
public class SubExample extends Example {
SubExample() {
FixVariable( 201 );
}

Which modifier should used in place of ****, so that it's subclass 'SubExample' saved in other package can access 'FixVariable()'  method of  it's  super class ?

1.private

2.public

3.protected

4.default(blank)

Answer :

2 & 3

Explanation :

 The protected modifier allows access by derived classes as well as classes in the same package. 

The private prevents any access from another class. 

The default access allows only access within the same package.

Ads