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