Hi i am educating myself oop principles. I would like to know if this is a correct example of Cardellis definition of parametric polymorphism. Please enlighten me.
The example is in cfml's script based syntax.
<cfscript>
Parent = createobject("component","webapp.model.Parent").init();
Child = createobject("component","webapp.model.Child").init();
GrandChild = createobject("component","webapp.model.GrandChild").init();
Test = createobject("component","webapp.model.DealWithObject");
dump(Test.getNumberOfParents(Parent));
dump(Test.getNumberOfParents(Child));
dump(Test.getNumberOfParents(GrandChild));
</cfscript>
<cfcomponent>
<cfscript>
// should deal with an infinte number of abstract data types (because of common structure)
public numeric function getNumberOfParents(component arg){
return -1 + arraylen(structfindkey(getmetadata(arguments.arg),"extends","all"));
}
</cfscript>
</cfcomponent>
No, just no.
Polymorphism means that you do not have to check what type something is, you just use it.
An example would be (C#):
public Boolean AreEqual(Object o1, Object o2)
{
return o1.Equals(o2);
}
The Method can accept any type of Object that inherits from Object (in C# almost everything) and Object implements Equals, so you can use it to make the check and don't have to check the type of any parameter.
Usually you accept some kind of interface to make sure the object supports the operation you want to perform.