Jaydeep Solanki
Jaydeep Solanki

Reputation: 2945

OOP inheritance issue

I have two classes, and the methods in them are shown below;

|----AVL----|     |-----RB------|
|           |     |             |
|           |     |             |
| - insert  |     | -balance    |
|           |     |             |
| - balance |     |             |
|           |     |             |
|-----------|     |-------------|

inside "insert" method of AVL, it calls "balance".
RB inherits AVL, so I can use insert method of AVL. Now when I call RB::insert(), it calls AVL::insert() & then AVL::balance(), but I want it to call RB::balance() from AVL::insert(), when a RB object calls "insert".

Upvotes: 0

Views: 36

Answers (1)

Jon
Jon

Reputation: 437336

This is a classic case for virtual methods: make AVL.balance virtual and override it in RB. The correct implementation will then be called depending on what type of object calls balance -- it doesn't matter that the code that calls balance will be written as part of AVL.

Upvotes: 4

Related Questions