aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Access specifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Access specifiers" Watch "Access specifiers" New topic
Author

Access specifiers

Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Hi, This is from Whizlabs mock exam. Requirements and the correct program is given below. Questions are in the comments.

1. You should be able to create instances of Alpha and Beta from outside packages in which they are defined.
2. When an Object of type Alpha(or any potential subclass of Alpha) has been created, the instance variable alpha may never be changed
3. The value of instance variable alpha must always be "A" for objects of type Alpha
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

What is the question asked in the mock exam? Have they given the 3 requirements and asked to select the correct implementation? And the questions in comments are your doubts?


Mohamed Sanaulla | My Blog
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Here you go mohamed.
What is the question asked in the mock exam? --> Need to add appropriate access specifier
Have they given the 3 requirements and asked to select the correct implementation? --> Yes they gave 3 requirements and asked to choose correct access specifer from private, default, protected and public
And the questions in comments are your doubts? --> Yes. They are my doubts.
Given program is the right program(correct answer)
mohamed sanaullah wrote:What is the question asked in the mock exam? Have they given the 3 requirements and asked to select the correct implementation? And the questions in comments are your doubts?
Saibabaa Pragada
Ranch Hand

Joined: Oct 24, 2010
Posts: 162
Could you please answer this question ?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Saibabaa Pragada wrote:1. You should be able to create instances of Alpha and Beta from outside packages in which they are defined.

For this I think the constructors have to be public. Also Beta should provide an default constructor (no arg constructor), otherwise we would not be able to create an instance of Beta class.

Saibabaa Pragada wrote:2. When an Object of type Alpha(or any potential subclass of Alpha) has been created, the instance variable alpha may never be changed

You can make the instance variable in Alpha class as private- and give no setters to change the instance variable.

Saibabaa Pragada wrote:3. The value of instance variable alpha must always be "A" for objects of type Alpha

So this means that- If you do a new Alpha()- The value of alpha="A". So this explains the constructors in class Alpha. I dont understand the reason behind the specific scopes of the constructors. May be there were other options which weren't closer to the requirement.
Samil Mehdiyev
Greenhorn

Joined: Jul 24, 2010
Posts: 1
if Alpha(String a) constructor is public then in a package other than "alpha" Alpha object can be created and its Alpha(String a) constructor can be called which violates third requirement. Being protected makes Alpha(String a) method protected in other packages.

This program doesn't seem to correct answer. Beta class is never going to be initialized because its constructor is private. So it breaks first requirement. May be you missed the program part.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access specifiers