Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

What is polymorphic hierarchy

Posted on 2007-04-05
11
663 Views
Last Modified: 2009-07-29
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
0
Comment
Question by:techbie
  • 6
  • 4
11 Comments
 
LVL 17

Expert Comment

by:rstaveley
ID: 18860418
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.
0
 
LVL 5

Author Comment

by:techbie
ID: 18868620
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
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 18869164
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).
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Author Comment

by:techbie
ID: 18873221
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
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 18874565
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.
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 18874572
NB: slicing isn't only an issue when a class is polymorphic, but the need for reference semantics is more apparent with polymorphic objects.
0
 
LVL 1

Expert Comment

by:ruizpauker
ID: 18895434
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
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 18895922
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
0
 
LVL 5

Author Comment

by:techbie
ID: 18899997
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
0
 
LVL 5

Author Comment

by:techbie
ID: 18925602
Hi all,

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

Thanks

Sijith
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 500 total points
ID: 18925888
Sorry I missed your previous post, Sijith.

> Is yes, then having a virtual function and implementing them in the base class would make it polymorphic heirarchy.?

Hmm.... Not a programming issue but an English language issue here. You wouldn't say it like that, but you would say "classes derived from that base had polymorphic hierarchy". I think you get the idea though.

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

No. Reference semantics are appropriate for polymorphism. Value semantics are a *problem*, because only the base class copy constructor is called when the copy is created, if you invoke a copy of the base like I did with get_a_slice_to_run(animal a). The get_a_slice_to_run function is a good example of value semantics working badly for polymorphism. I urge you to compile and execute that code to see what it does. [You get a slice of a human that is nothing more than an animal - i.e. with no legs. I hope I'm not offending any religions with my example!]

> 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.

If you get an animal to run and that animal is a human, you expect the animal to run with two legs. That's polymorphism. Polymorphism means that the code called in your base class invokes an implementation in the derived class. If you are using dymanic polymorphism the mechanism used is virtual functions. [If you are using static polymorphism the mechanism used is template expansion.]

Your best links are probably in Wikipedia. See http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming if you haven't already done so.

I believe that you are getting snagged on the word "hierarchy" and that may be an English language issue. If you understand polymorphism, you already understand hierarchy.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question