Given a class "Bar" that extends class "Foo" which implements interface "DeeDum"
public interface DeeDum {
public String getDee();
public String getDum();
}
public class Foo implements DeeDum {
public String dee = "D";
public String dum;
public String getDee() { return dee; }
public String getDum() { return dum; }
}
public class Bar extends Foo {
public String dee = "DEE";
public String dum = "DUM";
}
Why doesn't this work?
public static Bar mybar = new Bar();
Assert.assertEquals("DEE", mybar.getDee());
Assert.assertEquals("DUM", mybar.getDum());
I get "D" and null instead. In other words, Bar doesn't inherit accessors from Foo, and can't override properties. Somehow calling mybar.getDum() calls a static instance of class Foo and returns the static properties from the parent class. Even if the properties are overridden in the child class! Does that mean that you can't inherit any methods or properties?
I can't wrap my head around it. Why can't Java inherit accessors (and why did they choose such an odd alternative?)
Or am I just doing something wrong?
Actually, I'm seeing something weird and undeterministic still. If you have another class 'Bar' that extends Foo and sets the inherited accessors in an initialization block
While you can set the parent property in the above block, it doesn’t actually create a copy for the child class.
It seems to be a non-deterministic initialization for multiple classes.
So if you have Bar and Baz which both extend foo and have an initialization block, it seems like both inherit the value set by Bar.
public class Bar extends Foo {
{
dee = "dee";
dum = "dum";
}
}
public class Baz extends Foo {
{
dee = "DEE";
dum = "DUM";
}
}
public static Bar bar = new Bar();
public static Baz baz = new Baz();
System.out.println("mybaz: " + mybaz.getDee() + mybaz.getDum()); // DEEDUM
System.out.println("mybar: " + mybar.getDee() + mybar.getDum()); // DEEDUM
but if they're instantiated in a different order, I get:
public static Baz baz = new Baz();
public static Bar bar = new Bar();
System.out.println("mybaz: " + mybaz.getDee() + mybaz.getDum()); // deedum
System.out.println("mybar: " + mybar.getDee() + mybar.getDum()); // deedum
And it comes out still differently if a default is set in the base class Foo.
I think I understand now that the initialization block in Bar and Baz is actually setting Foo::dee and Foo::dum, but why the difference in declaration? Seems "undefined" to me.