Search code examples
multiple-inheritanceeiffel

how to merge an attribute in multi-inheritence


Repeatedly inheriting from 2 classes having the same parent, I fall into the classic case of inheriting 2 times of the same attribute. I'd like to merge the 2 attributes into one and tried to do it with an undefine, but it gets me a compile error. The other solution I see is renaming the attribute from one of both parents, but as I understand each instance of my D class would have an useless attribute which is not what I want...

Error: Undefine subclause lists name of frozen feature or attribute or C external. What to do: unless you can change the status of the feature in the parent, remove its name from Undefine subclause since it cannot be undefined.

How to merge 2 attributes from repeatedly inherited classes

class A
    serial: STRING

end -- class A

class B

inherit
    A

end -- class B


class C

inherit
    A

end -- class C


class D

inherit
    B
        undefine 
            serial -- error seems to appear here in that case
        end
    C

end -- class D

Solution

  • In case it's two unrelated attributes (not coming from the same parent) that you want to merge, you should redefine both of them:

    class A
    feature
        serial: STRING
    end
    
    class B
    feature
        serial: STRING
    end
    
    class C
    inherit
        A
             redefine
                   serial
             end
        B
             redefine
                   serial
             end
    feature
        serial: STRING
    end
    

    As you already saw, the compiler will not let you undefine an attribute, even when the goal is to merge it with another attribute.