Java Programming - Declarations and Access Control - Discussion

Discussion Forum : Declarations and Access Control - General Questions (Q.No. 1)
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?
public
private
protected
transient
Answer: Option
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.

Discussion:
10 comments Page 1 of 1.

Bittu said:   8 years ago
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.
(2)

SREEJITH.K said:   1 decade ago
Thanks. Very nice tutorial.

Suji said:   1 decade ago
Thanks. It's very useful me

RAHUL said:   1 decade ago
Please explain " the subclasses clause in the question eliminates default" ?.

Akash said:   1 decade ago
Why not the second answer is public?

Sam said:   1 decade ago
Private - Native.

Default - Package access.

Protected - Inherit access.

Public - Universe.

Piyali said:   10 years ago
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:   8 years ago
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.

Preethi said:   6 years ago
I can't understand this answer. Please clearly explain.

Vishal gupta said:   6 years ago
This can be solved if there is any subclass which is protected.

Post your comments here:

Your comments will be displayed after verification.