jim smithson
jim smithson

Reputation: 1

inherit from two types in Ada

suppose i have:

GENERIC
   TYPE Item IS PRIVATE;
   PACKAGE Abstract_something IS
      TYPE something IS ABSTRACT TAGGED LIMITED PRIVATE;
      procedure x(...)IS ABSTRACT;
      procedure y(...)IS ABSTRACT;
      PRIVATE
         TYPE something IS ABSTRACT TAGGED LIMITED NULL RECORD;
END Abstract_something;

then i make two children

1.

GENERIC
   PACKAGE Abstract_something.Child IS
      TYPE something_2  IS ABSTRACT NEW something WITH PRIVATE;
      PROCEDURE x(...);
      PROCEDURE y(...);
      FUNCTION  xx(...) RETURN whatever1;
      error: EXCEPTION;
      PRIVATE
         TYPE something_2  IS ABSTRACT NEW something WITH RECORD
            some declarations here..
         END RECORD;
END Abstract_something.Child;

2.

GENERIC
PACKAGE Abstract_something.Child2 IS
   TYPE something3 IS ABSTRACT NEW something WITH PRIVATE;
   PROCEDURE z ( ... ) IS ABSTRACT;
   PRIVATE
      TYPE something3 IS ABSTRACT NEW something WITH NULL RECORD;
END Abstract_something.Child2;

here both child and child2 inherit from same parent and i want to create child3 that has type something4 that is identical to something2 and adds to it procedure Z from something3. can it be done? and how?

thanks.

Upvotes: 0

Views: 427

Answers (2)

flyx
flyx

Reputation: 39668

You can use orthogonal inheritance, it might apply to your use case. You'd have to change Child2 to:

GENERIC
   TYPE base IS ABSTRACT NEW something WITH PRIVATE;
PACKAGE Abstract_something.Child2 IS
   TYPE something3 IS ABSTRACT NEW base AND something3_interface WITH PRIVATE;
   PROCEDURE z ( ... ) IS ABSTRACT;
   PRIVATE
      TYPE something3 IS ABSTRACT NEW base AND something3_interface WITH NULL RECORD;
END Abstract_something.Child2;

Now the additions in something3 can be applied to any type that inherits from something - you just have to instantiate Abstract_something.Child2 with the base you want to inherit from - like e.g. something_2.

something3_interface is optional and would have to be added to Abstract_something:

...
TYPE something3_interface IS INTERFACE;
-- Possibly some primitive operations defined by something3_interface here
...

You need the interface something3_interface only if you want to use the type something3 somewhere without specifying an instantiation of Abstract_something.Child2 - as its generic package has an own parameter, you cannot use something3 directly outside of Abstract_something.Child2.

Upvotes: 0

Ansgar Esztermann
Ansgar Esztermann

Reputation: 251

Ada does not support multiple inheritance, except for "Interfaces" in Ada 2005, which however do not have an associated type. From what you've written, you could use that concept by making an interface of something3 (it has a null record anyway, even if you do not expose that in your example).

Does something3 really need to inherit from Abstract_something? If not, you could do

type something3 is interface;
type something4 is new something2 and something3 with private; -- or with null record etc

From my experience, it pays to think carefully about what the properties of the problem at hand really are (as opposed to those of the implementation): in the case of inheritance, does an "is-a" relationship exist throughout the hierarchy? That is, when B and C inherit from A, and D inherits from both B and C, is every B and C also an A? Is every D really both a B and a C? "Has-a" does not lend itself to inheritance (although the unwary may implement it that way).

Upvotes: 1

Related Questions