Link to home
Start Free TrialLog in
Avatar of poffel
poffel

asked on

Inherited Methods return null

Hi,

I wrote a class B that extends another class A I wrote.
Class B inherits some mehtods from Class A.

The methods are get-methods that basically return values from some private String-variables:

    public String getDescription () {
         return this.sDescription;
    }

When I call the methods from Class A in Class A they return the value stored in "sDescription", so far everything is fine.

When I call an inherited method in Class B it only returns null.

In Class B I tried the following:

System.out.println( "method:" + getDescription() );
-> prints "method: null" to console

System.out.println( "field:" + this.sDescription );
-> prints "field: <value of sDescription>" to console


It seems the inherited method doesn't access the variable from class B but the one from class A which isn't initialized at that moment.

How can I tell the method to give back the value stored in the variable from the instanciated class (instance of class B) it was called from?

Hope anybody can help me with this,
regards,
paul
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Are you replacing the instance variable in your subclass? If so you shouldn't. e.g.

class A {
 
  String description;  
 
  public class A() {
    description = "default";
  }

  public String getDescription() {
    return description;
  }
}

class B extends A {
 
  //String description;  

  public B() {
    super();
  }

  public String getDescription() {
    return description;
    // if you uncomment the description field, you'll get null, as opposed to 'default'
  }
}
Avatar of poffel
poffel

ASKER

you are right.

but I used

   private String description;

for the declaration of the variable in Class A, to make sure the variables can only be changed using the provided methods.
When I use private methods I can't access them from Class B, if I try to do so I get
"Description has private access in <Class B>"
If I only use

   String description;

the variable could be changed without using the provided methods...or am I wrong here?
I simply left out the 'private' through carelessness ;-)
It should be there yes.
Avatar of poffel

ASKER

well, but if I use the "private" declaration, I get an compiler error saying:
"Description has private access in <Class B>"

is it possible at all to use access a private variable from another class? In my application I can't access them, even from my sub-class (Class B).

I declared the variables as protected now, but this way they appear in the Api Documentation and they could be changes from all other classes in the same package, actually I only wanted it to be accessable from Class B.

I think there is a way to allow access to variables only for certain classes, it is something wirt "level..." I think, did anybody hear about that?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
poffel:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.