Re: private instance variable of derived class not always returned by base class getter?
On 31 Mrz., 14:21, Lothar Kimmeringer <news200...@kimmeringer.de>
wrote:
Piet wrote:
I have
found that I can get what I want if, in the parameterless constructor
of "Derived", the call to super() is rewritten to "super("Name for
constructor without arguments")", but I still do not fully understand
why this makes a difference.
Because that constructor set the private member in base.
That way you can leave away the private member in the
derived class completely.
....in which case I would not be able to access "this.name" from
anywhere in Derived. I didn=B4t even need to extend "Base" but could be
happy with "Base" instances only.
I played around with my "Base" and "Derived" classes further. Here is
my last version. Now I think that I "know" what to set where to get
what I want, but I would not say that I have "understood" it
technnically:
//code
public class InheritanceConstructorTest{
public static void main(String[] args){
Base b_default_name = new Base();
Base b_with_name = new Base("base with explicit name");
Derived d_default_name = new Derived();
Derived d_with_name = new Derived("derived with explicit name");
System.out.println("d_default_name name:
"+d_default_name.getName());
System.out.println("d_with_name name: "+d_with_name.getName());
System.out.println("d_with_name, from method declared in derived:
"+d_with_name.getNameFromDerived());
System.out.println("b_default_name name:
"+b_default_name.getName());
System.out.println("b_with_name name: "+b_with_name.getName());
}
}
class Base{
private String name;
Base(String name){
this.name = name;
}
Base(){
this.name = "base without explicit name";
}
public String getName(){
return this.name;
}
}
class Derived extends Base{
private String name;//line 27
Derived(String name){
super(name);//line 29
this.name = name+", bound in Derived";//line 30
}
Derived(){
super("derived without explicit name");//line 33
this.name = "derived without explicit name, bound in Derived";//line
34
}
public String getNameFromDerived(){
return this.name;
}
}
//end of code
I got the following output:
d_default_name name: derived without explicit name
d_with_name name: derived with explicit name
d_with_name, from method declared in derived: derived with explicit
name, bound
in Derived
b_default_name name: base without explicit name
b_with_name name: base with explicit name
Conclusions:
1. If I want to access a private instance variable (declared in a
derived class) by a method declared in the superclass, binding of that
private variable to the instance of the derived class must occur in a
base class method, i. e. I must call a "super" method at some point.
2. If I want to access a private instance variable (declared in a
derived class) by a method declared in the derived class (which amy
override a superclass method), it must be explicitly bound to the
instance inside a method declared in the derived class.
I never had to deal with inheritance issues like that, but now, the
more I think about it, the more sense does it make.
Many thanks!
Piet71