Java Programming - Declarations and Access Control - Discussion

1. 

You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?

[A]. public
[B]. private
[C]. protected
[D]. transient

Answer: Option C

Explanation:

Access modifiers dictate which classes, not which instances, may access features.

Methods and variables are collectively known as members. Method and variable members are given access control in exactly the same way.

private makes a member accessible only from within its own class

protected makes a member accessible only to classes in the same package or subclass of the class

default access is very similar to protected (make sure you spot the difference) default access makes a member accessible only to classes in the same package.

public means that all other classes regardless of the package that they belong to, can access the member (assuming the class itself is visible)

final makes it impossible to extend a class, when applied to a method it prevents a method from being overridden in a subclass, when applied to a variable it makes it impossible to reinitialise a variable once it has been initialised

abstract declares a method that has not been implemented.

transient indicates that a variable is not part of the persistent state of an object.

volatile indicates that a thread must reconcile its working copy of the field with the master copy every time it accesses the variable.

After examining the above it should be obvious that the access modifier that provides the most restrictions for methods to be accessed from the subclasses of the class from another package is C - protected. A is also a contender but C is more restrictive, B would be the answer if the constraint was the "same package" instead of "any package" in other words the subclasses clause in the question eliminates default.


Sreejith.K said: (Jan 27, 2011)  
Thanks. Very nice tutorial.

Suji said: (Jun 24, 2011)  
Thanks. It's very useful me

Rahul said: (Aug 22, 2011)  
Please explain " the subclasses clause in the question eliminates default" ?.

Akash said: (Jul 18, 2013)  
Why not the second answer is public?

Sam said: (Nov 8, 2014)  
Private - Native.

Default - Package access.

Protected - Inherit access.

Public - Universe.

Piyali said: (Dec 23, 2014)  
If any class declared as private, other classes even in the same package, can not access the member of private class by composition. So, in the above question if the constraint was the "same package" instead of "any package" answer should not be option 'B'.

Msheliga said: (Jan 28, 2017)  
I believe the following statement is incorrect as option B is private, not default.

B (private) would be the answer if the constraint was the "same package" instead of "any package" in other words the subclasses clause in the question eliminates default.

Bittu said: (Apr 27, 2017)  
I don't agree with this answer. The Answer should have been A] public, as the question says- subclasses in any package. Seems like the question should have been bitten clearly asked.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.