Link to home
Start Free TrialLog in
Avatar of techbie
techbie

asked on

What is polymorphic hierarchy

Hi all,

I was reading one of the papers I got from IEEE regarding polymorphism.
 It said about polymorphic heirarchy and something called core interface. But I was unable to understand what they are telling.

http://csdl.computer.org/dl/proceedings/apaqs/2000/0825/00/08250053.pdf

I know about heirarchy in c++ and polymorphism, but dont know about polymorphic heirarchy.

what is the difference between polymorphic and non-polymorphic heirarchy in c++.

thanks in advance
sijith
Avatar of rstaveley
rstaveley
Flag of United Kingdom of Great Britain and Northern Ireland image

If you know about heirarchy in c++ and polymorphism, you know about polymorphic heirarchy. Either you've over-simplified the former or you are assuming things are more complicated than they are in the latter.
Avatar of techbie
techbie

ASKER

hi,

in inheritance we have a base class and a derived class. In polymorphism, we have different functions with the same name but different parameter.  

I will give my understanding of polymorphic heirarchy and non-polymorphic heirarchy . just correct me if i am wrong.

In polymorphism, there is a concept called core interface. This is a set of polymorphic methods that several classes share. For a hierarchy to be in polymorphic hierarchy, all its classes must share a core interface. If these classes don’t share a core interface, then it is called non-polymorphic hierarchy.

Let S be a set of classes. Then hierarchy S is in polymorphic if there exist a core class. This core class may or may not belong to the S set. All the classes in the hierarchy should share this interface.

Is my understanding of polymorphic hierarchy correct ?

thanks
I suspect that you haven't completely clicked with the power of polymorphism.

Polymorphism is to most people exemplified by dynamic polymorphism rather than static polymorphism.

Dynamic polymorphism is where a reference or pointer to a base class is used to call a *virtual* method which is overriden in the derived class instance which the pointer/reference actually points to. The virtual method points to the appropriate implementation for the derived object. It means that you can write code for an interface defined by the base class and make use of overrides in derived classes.

There is a simple example in http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

Static polymorphism is the stuff of templates. It is ultimately similar in many ways, but unlikely to help your understanding. [My advice: Ignore it for now.]

To understand polymophic hierarchy is to get your head around the implications of polymorphism (hard) and then get your head around hierarchy (easy).
Avatar of techbie

ASKER

Hi,

but what exactly is polymorphic heirarchy. Is it another name for dynamic polymorphism.  

I know that if we declare a virtual function in the base class, we can have their implementation in the derived class. Compiler will see if the function is declared in the derived class. If yes, it takes from the derived class. Else it takes from base class. Is this polymoprphic heirarchy ?

In that case, to see if a heirarchy is polymorphic, I need to open the base class to see if it has declared any function as virtual. Then  check the derived class to see if the function is also declared and defined in the derived class. In this case, I conclude it as a polymorphic heirarchy. Else it is non-polymorphic heirarchy.

please correct me if i am wrong.

Thanks
Sijith
Polymorphic hierarchy is hierarchy that is polymorphic. That's all.

If you are looking for a text-book definition, you'd be wrong to ignore static polymorphism. However, if you want to understand polymorphism, you can ignore static polymorphism until you get up and running with generic programming (i.e. templates).

There are some important considerations in dynamic polymorphism. In order to work with dynamic polymorphism you cannot work with copy by value (or what text books call "value semantics"), because you wind up with polymorphic objects being "sliced". You need references or pointers.

--------8<--------
#include <iostream>
#include <string>
using namespace std;

class animal {
protected:
      const string name;
public:
      animal(const string& name) : name(name) {}
      virtual void run() const {
            cout << name << " can't run\n";
      }
};

class runner : public animal {
      const int legs;
public:
      runner(const string& name,int legs) : animal(name),legs(legs) {}
      void run() const {
            cout << name << " runs with " << legs << " legs\n";
      }
};

void get_it_to_run(const animal& a)
{
      a.run();
}

// Value semantics here result in the derived animal being sliced
void get_a_slice_to_run(animal a)
{
      get_it_to_run(a);
}

// Reference semantics leave the derived animal intact
void get_a_reference_to_run(animal& a)
{
      get_it_to_run(a);
}

int main()
{
      animal worm("worm");
      runner person("person",2);
      get_it_to_run(worm);
      get_it_to_run(person);
      get_a_slice_to_run(person);
      get_a_reference_to_run(person);
}
--------8<--------

Nasty business slicing a person.
NB: slicing isn't only an issue when a class is polymorphic, but the need for reference semantics is more apparent with polymorphic objects.
Is this question still open?

I could give you some information on inheritance, static inheritance, virtual inheritance, abstract classes, interfaces, virtual functions and pure virtual functions.

Paul Ruiz Pauker
Hi Paul,

If you reckon that directing techbie to use Wikipedia or some other ref to look up those terms would answer http:#a18873221 or the main question, go for it. It is still open. techbie says that he is OK with polymorphism and hierarchy, but wanting to sort out what polymorphic hierarchy is. I've had a go at answering his question, but we're all human!

     Rob
Avatar of techbie

ASKER

Hi,

I went through the code given by rstaveley.  Is this code having polymorphic heirarchy. Is yes, then having a virtual function and implementing them in the base class would make it polymorphic heirarchy.?

I also note that to be a class in polymorphic heriarchy, the call by value needs to be called.  is it correct ?

 If a person look at a code, how can he know (few things to look at) to confirm that the class heirarchy is a polymorphic heirarchy.

I am basically looking for clear cut difference between a polymorphic and non-polymorphic heirarchy.
like - polymorphic heirarchy has this while non-polymorphic heirarchy dont have this. etc.

thanks in advance

sijith
Avatar of techbie

ASKER

Hi all,

Can anyone give me some good links where I can learn more about polymorphic and non-polymorphic heirarchy

Thanks

Sijith
ASKER CERTIFIED SOLUTION
Avatar of rstaveley
rstaveley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial