?
Solved

Inherited Methods return null

Posted on 2003-03-15
6
Medium Priority
?
173 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:poffel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8144617
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'
  }
}
0
 

Author Comment

by:poffel
ID: 8144713
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?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8144723
I simply left out the 'private' through carelessness ;-)
It should be there yes.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:poffel
ID: 8144793
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?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 150 total points
ID: 8144868
Yes, you're right. It depends what you want to do. If you want access to the instance variables in a subclass, then package access is one way to go. You can also simulate this by doing something like the following:

public class TestAandB {

  public static void main(String[] args){
    A a = new A();
    System.out.println(a.getDescription());
    B b = new B();
    System.out.println(b.getDescription());
  }

}


class A {

 private String description;

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

 public String getDescription() {
   return description;
 }
}

class B extends A {

 //String description;

 public B() {
   super();
 }

 // What to do if you want to alter the superclass behaviour
 public String getDescription() {
   // Use inverted commas
   return "'" + super.getDescription() + "'";
 }

}
0
 

Expert Comment

by:CleanupPing
ID: 9058671
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.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month9 days, 6 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question