Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

simple access modifier question....

Thread view: 
. - 20 Feb 2005 09:18 GMT
I have the following declarations in a class I have written:

public class SecretWord {
  private String description;
  private String secret;

//constructor
  public SecretWord(String startDescription, String startSecret){
     description = startDescription;
     secret = startSecret;
  }

In a different class I have:

public class SecretWordII extends SecretWord {

//constructor
  public SecretWordII(String startDescription, String startSecret){
     super(startDescription, startSecret);
  }

and later on in SecretWordII I have:

out.println(description+":"+secret);

Whe I try to compile the class I get:

.\SecretWordII.java:24: description has private access in SecretWord
     out.println(description+":"+secret);
                 ^
.\SecretWordII.java:24: secret has private access in SecretWord
     out.println(description+":"+secret);

WHY??
Can't I access private variables using super() ?

Thanks in advance

Michael
Anthony Borla - 20 Feb 2005 09:36 GMT
> I have the following declarations in a class I have written:

<SNIP>

> .\SecretWordII.java:24: description has private access in
> SecretWord
[quoted text clipped - 8 lines]
>
> Thanks in advance

A 'private' member, be it attribute or method, is just that: 'private' to
the class. To allow others access to 'private' members you would provide
accessor methods.

Otherwise, you must alter the access modifier, changing it to package-level
or 'protected' access if you wish classes in the same package or subclasses
to be allowed access.

I hope this helps.

Anthony Borla
Hal Rosser - 20 Feb 2005 21:25 GMT
when you tried to print the private variable directly, that was the problem.

in the class - you need "public access methods" to access the private
variables.

public String getDescription(){
    return description;
}

Then - to use this method

out.println(getDescription() ); // since getDescription() returns a String -
you're ok

The constructor you created were used to "set" the private variables in your
class.
Anzime - 20 Feb 2005 22:10 GMT
A subclass inherits public and protected members of its super class.
Signature

Regards,
Anzime

> I have the following declarations in a class I have written:
>
[quoted text clipped - 35 lines]
>
> Michael
Anzime - 20 Feb 2005 22:11 GMT
A subclass inherits public and protected super class members.
Signature

Regards,
Anzime

> I have the following declarations in a class I have written:
>
[quoted text clipped - 35 lines]
>
> Michael


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.