Skip to content

Concept : inheritance flattening

stephanenicolas edited this page Sep 15, 2013 · 7 revisions

BoundBox "flattens" the fields and methods inherited from super classes in order to provide a more convenient API when using a class BoundBoxOfFoo.

Suppose we have 3 classes A, B & C such as :

public class A {
 private Bar bar;
 private Qutz qutz;
}

public class B extends A {
 private Foo foo;
 private Bar bar;
}

public class C extends B {
 private Foo foo;
}

We can represent those classes in the left-handside schema below. The Y-Axis represents hierarchy level, the X-Axis represents the different fields. The red-crosses represent the hierarchy level at which a field is defined.

Inheritance Flatening examplified

When you create BoundBoxOfC (by typing @BoundBox( boundClass=C.class ) in your code, BoundBoxOfC will flatten hierarchy of declarations of the fields of C and its super class. As illustrated by right-handside schema above, all fields of super classes that are not hidden in their subclasses will be presented as if they were defined in Citself.

Thus you can use :

@BoundBox( boundClass=C.class)
public class CTest {
 BoundBoxOfC boundBoxOfC = new BoundBoxOfC( new C() );

 public void testInheritanceFlattening() {
    assertEquals( null, boundBoxOfC.boundBox_getFoo() );
    assertEquals( null, boundBoxOfC.boundBox_getBar() );
    assertEquals( null, boundBoxOfC.boundBox_getQutz() );
 }

 public void testHiddenFields() {
    assertEquals( null, boundBoxOfC.boundBox_super_B_getFoo() );
    assertEquals( null, boundBoxOfC.boundBox_super_A_getBar() );
 }
}

Note that in the second test, we access fields that are defined in super classes but hidden. Thus, we still need to specify the class that defines them.

Without inheritance flattening, you would have to specify in which class each field is defined, like this :

@BoundBox( boundClass=C.class)
public class CTest {
 BoundBoxOfC boundBoxOfC = new BoundBoxOfC( new C() );

 public void testWithoutInheritanceFlattening() {
    //warning this will not compile as inheritance flattening will create a more convenient API.
    assertEquals( null, boundBoxOfC.boundBox_getFoo() );
    assertEquals( null, boundBoxOfC.boundBox_super_B_getBar() );
    assertEquals( null, boundBoxOfC.boundBox_super_A_getQutz() );
 }

 public void testHiddenFields() {
    assertEquals( null, boundBoxOfC.boundBox_super_B_getFoo() );
    assertEquals( null, boundBoxOfC.boundBox_super_A_getBar() );
 }
}